Can someone help me achieve multiple zend pagination in a view using ajax. I have managed to achieve single pagination no problem, but now i want to perform more than 1.
this is my setup:-
added to the bootstrap:-
public function _initPaginator(){
Zend_Paginator::setDefaultScrollingStyle('Sliding');
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_control.phtml');
}
'pagination_control.phtml' has been taken from the zend framework manual.
added to the controller:-
public function init()
{
parent::init();
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('view', 'html')
->initContext();
}
added this to the controller action:-
public function viewAction()
{
$query = $this->_em->createQueryBuilder()
->select('t')
->from('Ajfit\Entity\Ticket', 't')
->where('t.engineerFk = :engineer')
->orderBy('t.dt', 'desc')
->setParameter('engineer', $engineer)
->getQuery();
$paginator = new Paginator($query);
$adapter->getIterator();
$zend_paginator = new \Zend_Paginator($adapter);
$zend_paginator->setItemCountPerPage(3)
->setCurrentPageNumber($this->_getParam('page'));
$this->view->ticketPaginator = $zend_paginator;
}
added this to the view:-
<div>
<div id="ticket-history">
<?php
echo $this->render('profile/view.ajax.phtml');
?>
</div>
</div>
<script>
$(document).ready(function() {
$('.pagination-control').find('a').live('click', function(e) {
var link = $(this);
$('#ticket-history').load(link.attr('href'), { format: 'html' });
return false;
});
});
</script>
my 'profile/view.ajax.phtml' script contains:-
<?php
echo '<table border="0" width="100%" cellspacing="3" cellpadding="3"><tr>';
foreach($this->ticketPaginator as $ticket){
echo '<td>' . $ticket->getSubject() . '</td>';
}
echo '</tr></table>';
?>
<?php
echo $this->paginationControl($this->ticketPaginator);
?>
This all works fine, however, how would one go about adding a second or third paginator to this view for a different doctrine entity?
Any help would be much apprieciated.
Thanks
Andrew
for that you have to use multiple addActionContext in init()
$this->_helper->ajaxContext->addActionContext('view', 'html')->initContext();
$this->_helper->ajaxContext->addActionContext('list', 'html')->initContext();
and you can continue
add action in controller as listAction() for paginator create file as list.ajax.phtml also and add required code inside them
This problem can be resolved if you manipulate the pagination links to let it call the other action.
For example: In the View Part, you can use the fourth parameter of the paginationControl method to define the links which refers to another action. This link should then be called in pagination_control.phtml.
Related
I know how AJAX call to CodeIgniter is working.
Edit for suggested answer
I tried redirection as a possible solution to my problem. I am not asking it as question. I am having problem passing parameter through URL and reusing view containing ajax post call. Also I am not aware how to post single value state_id which is already part of new_admission form.
I am loading city names for state_id of branch dynamically on $(document).ready()
When I call load_city() from admission() it works perfect.
XHR for load_city goes like:
localhost/project_folder/controller/load_city
But when I try edit_admission($studentId) it loads admission_view recursively inside <div id="city"></div>.
XHR for load_city goes like:
localhost/project_folder/controller/edit_admission/load_city
Here load_city is considered as parameter and edit_admission() is called recursively.
Controller:
public function load_city()
{
//load cities to $data from model
$this->load->view('city_view', $data);
}
public function admission()
{
//load init data for admission view from model
$this->load->view('admission_view', $data);
}
public function edit_admission($studentId)
{
//load init data for admission view from model
$this->load->view('admission_view', $data);
}
AJAX code:
function get_city() {
var parameters = {}; //instantiate the array
parameters['state_id'] = state_id;
$('#divcity').load('load_city', parameters, function (data) {
$('#city').combobox();
//code
});
};
load_city view:
<?php
echo '<select class="'.'form-control chzn-select col-lg-8 required'.'" id="'.'city'.'" name="'.'city'.'">';
echo '<option></option>';
foreach ($init_city['city_names'] as $row)
{
echo '<option value="'.$row->city_id.'">' .$row->city_name.'</option>';
}
echo '</select>';
?>
admission_view:
<div class="form-group">
<label class="control-label col-lg-4">City</label>
<div class="col-lg-8" id="divcity" name="divcity"></div>
</div>
I tried
public function edit_admission($studentId)
{
if(intval($studentId) > 0){
//load init data for admission view from model
$this->load->view('admission_view', $data);
} else if($studentId == 'load_city'){
//not passing post data to load_city()
redirect('branch/load_city');
}
}
Above code is not passing post data to load_city().
Also, I tried adding bellow code in routes.php
$route['edit_admission/load_city'] = 'load_city';
I did not get any success in both cases.
I try to render an action with a dedicated view in a modal window with an ajax call in Zend Framework 2.
This is my controller action :
public function myAction()
{
$htmlViewPart = new ViewModel();
$htmlViewPart->setTemplate('path/to/my/view')
->setTerminal(true)
->setVariables(['arrayVar' => ['a', 'b', 'c']]);
return $htmlViewPart;
}
The view :
<?php
foreach($arrayVar as $k => $v)
{
echo $k . ':' . $v . '<br>';
}
The js :
$(".my-modal-link").click(function() {
$('#myModal .modal-body').load($(this).data('/url/to/my/action'));
});
This not do the trick. I also tried with a JSON model too:
public function myAction()
{
$htmlViewPart = new ViewModel();
$htmlViewPart->setTemplate('path/to/my/view')
->setTerminal(true)
->setVariables(['arrayVar' => ['a', 'b', 'c']]);
$htmlOutput = $this->getServiceLocator()->get('viewrenderer')->render($htmlViewPart);
$jsonModel = new JsonModel();
$jsonModel->setVariables(['html' => $htmlOutput]);
return $jsonModel;
}
But the final render in the modal is something like :
{"html":"0:a\u003Cbr\u003E1:b\u003Cbr\u003E2:c\u003Cbr\u003E"}
Have an idea to how achieve that?
All you need is disabling the layout using setTerminal() and returning proper model from your controller to render HTML output in your modal.
In your case, you have to return a ViewModel instance. So, your first approach is correct. This should work:
$htmlViewPart = new ViewModel();
$htmlViewPart->setTemplate('path/to/my/view')
->setTerminal(true)
->setVariables(['arrayVar' => ['a', 'b', 'c']]);
return $htmlViewPart;
The second case is; you're trying to use a HTML output as json data. Try to change your ajax loading mechanism similar to this:
$(".my-modal-link").click(function() {
$.get("/url/to/my/action", function(data) {
$('#myModal .modal-body').html(data);
});
});
This will be append the rendered output (which doesnt have a layout) into modal's body. You may also want to read this for JsonModel scenario.
Hope it helps.
I found a solution.
Just create an empty layout returning content.
// Application/view/layout/ajax.phtml
<?php
echo $this->content;
And set this template in the action view
<?php
$this->layout()->setTemplate('layout/ajax');
It works now with Jquery $.load() / ViewModel strategy
Have the same problem this works...
$.get("<?php echo $this->url('your url'); ?>", function(data)
{
console.log(data);
$('#myModal .modal-body').html(data);
});
I am having difficulty getting the correct URL when I call a method to load a view.
Heres my controller:
public function post() {
$title = $this->input->post('title');
$data = $this->p_Model->post($title);
$this->qs($data);
}
public function qs($id){
$title = $this->s_Model->getTitle($id);
$result = $title->result();
$this->load->view('q_View', array('results' => $result));
}
Heres my view:(note this view is not the view which gets loaded from the qs function, but one which calls the qs function)
<html>
<body>
<table>
<?php
if (isset($qs)) {
foreach ($qs as $row) {
$id = $row->qID;
echo '<a href="'.site_url('myController/qs/'.$id).'">';
echo $row->title;
echo "<br>";
}
}
?>
</table>
</body>
</html>
So in my controller I have two functions, the qs function works separately by itself so can be called in the view and give the following url myController/qs/1 however when I use the post function I get a url like this myController/post so my question is how can I get my url to be like the first example?
Instead of using the line:
$this->qs($data);
You can use a redirect:
redirect('/mainController/qs/'.$data);
That should work in the same way that you have used in your view
Try base_url and also you can use current_url() returns the full URL (including segments) of the page being currently viewed.
echo '<a href="'.base_url('myController/qs/'.$id).'">';
I want to pass a language id for each link i click from my view to controller.
My view code is
<?php foreach ($languages as $lang) { ?>
<li>
</li>
<?php } ?>
My controller is
public function box($box_id=null, $language_name=null, $language_id=null) {
/// my function code
echo $box_id;
echo $language_name;
echo $language_id;
$data['languages'] = $this->Home_model->getLanguages($box_id);
}
The languages array contain the language id and language name
i want the name to be in url but not the id
The url looks like this
http://localhost/mediabox/home/box/12/en
if i send the language id in url it is then visible otherwise it is not visible in the controller.
How can I get language id for each link in controller without sending it in url
Thanks
pass the language name in the url without the ID, compare to languag_name column in table.
Lets assume you have url: http://localhost/mediabox/home/box/en
controller
<?php
# I wont write a controller but you should know how to do that, im also writing code as if you are just focusing on getting language.
public function box( /**pass in your other uri params as needed **/ $lang_name = 'en'){
#you could load this in the constructor so you dont have to load it each time, or in autoload.php if your using it site wide.
$this->load->model('lang_model', 'langModel');
#this example shows loading the library and running the function
$this->load->library('lang_library');
$this->lang_library->_getLang($lang);
#this example shows putting the getLang function inside the controller itsself.
self::_getLang($lang);
}
library/private function
<?php
private functon _getLang($lang = 'en'){
#run the query to retrieve the lang based on the lang_name, returns object of lang incl id
$lang = $this->langModel->getLang($lang_name);
if (!$lang){
die('language not found');
}else{
return $lang;
}
lang model
<?php
public function getLang($lang_name = 'en'){
$this->db->where('lang_name', $lang_name);
$this->db->limit(1);
$q = $this->db->get('languages');
if ($q->mysql_num_rows > 0){
return $q->result();
}else{
return false;
}
}
you will then have a variable with object associated to it then you can simply call $lang->lang_name; or $lang->lang_id;
Session storage
<?php
#you could call this in the beginning after using an ajax `$.post();` to retrieve the ID.. the easiest route though is whats above. I use this in my REST APIs
$this->session->set_userdata('lang', $lang);
Your confusion is in 'passing back' to the controller. Don't think of it as from controller => View (passing it say $data['something'] variables).
Its basically a <form>, so take a look at form helper and then at form validation.
That will give you an idea of how to create a form using codeigniter syntax.
In your controller, you would do a validation, and if it matches the language (or whatever you submit), then you can utilize sessions to save it for every page (so you don't need it in the URL).
Sessions are very simple and saving an item is as easy as:
$this->session->set_userdata('varname', 'value');
Later, on every other controller, you can check the variable
$language = $this->session->userdata('varname');
// load language etc;
Make a table with language ID and Language name in the database, just pass the language name to the controller and get the language ID by doing a db call.
You could do it using jQuery. Something like;
In View;
<ul id="language_selector">
<?php foreach ($languages as $lang) { ?>
<li>
<a href="javascript:;" class="change-language" data-language="<?php echo $lang['language_name']?>" data-box="<?php echo $template_data['box_id']?>">
<img src="<?php echo base_url(); ?>public/default/version01/images/country_<?php echo $lang['language_name'] ?>.png" width="27" height="18" border="0" />
</a>
</li>
<?php } ?>
</ul>
The JS;
$(function() {
$('.change-language').live('click', function() {
var language_name = $(this).data('language');
var box_id = $(this).data('box');
$.ajax({
url: '/home/box/'+language_name,
type: 'post',
data: 'box_id='+box_id,
success: function( data ) {
self.parent.location.reload();
},
error: function( data ) {
alert('oops, try again');
}
});
});
});
The controller:
public function box($language) {
$box_id = $this->input->post('box_id');
// do a llokup on the language as suggested by #vivek
}
You are telling CodeIgniter that you will be receiving both the language name and id in your action.
public function box($box_id=null, $language_name=null, $language_id=null) {
}
Change that to just
public function box($box_id=null, $language_name=null) {
}
For your example URL you should then get $box_id == 12 and $language_name == 'en'.
Then lookup the language id by using it's name either in a helper or as part of a Language model as Mike suggests.
i installed the plugin and cannot find anything that tells me how it works and how to use it.
i have the icon in place and an empty onClick event
in homeSuccess.php:
<input class='submit_img' type="image" src="/images/rainbow/feed-icon-14x14.png" value="Feed" alt="Feed" onClick="gotoFeed(this.value,<?php echo $usr_profile->getId();?>)">
gotoFeed JS in homeSuccess.php:
function gotoFeed(id)
{
console.log("testing");
//must redirect to feeds page??
}
in the actions class:
public function executeFeed(sfWebRequest $request)
{
$profile_id = $this->getUser()->getAttribute('profile_id','zero');
}
can anybody help please?
thanks
You can find the complete tutorial at this link sfFeed2Plugin, just clicking the tab Readme
UPDATE
I'm using this plugin with an action like this in my action class:
public function executeFeedRss(sfWebRequest $request)
{
$feed = new sfRss201Feed();
$feed->setTitle('MySite');
$feed->setLink($this->generateUrl('#homepage'));
$c = new Criteria;
$c->addDescendingOrderByColumn(PostPeer::CREATED_AT);
$c->setLimit(10);
$Posts = PostPeer::doSelect($c);
foreach ($Posts as $post)
{
$item = new sfFeedItem();
$item->setTitle($post->getTitle());
// according to routing rule
$item->setLink($this->generateUrl('#posts', array('id'=>$post->getId())));
$item->setPubdate($post->getCreatedAt('U'));
$item->setUniqueId($post->getSlug());
$item->setDescription($post->getBody());
$feed->addItem($item);
}
$this->feed = $feed;
}
and this code inside the template feedRssSuccess.php:
<?php decorate_with(false) ?>
<?php echo $feed->asXml(ESC_RAW) ?>
Finally simply I have a link to this action in my layout through a template, but of course in a pageSuccess.php is the same.
I hope this can help you.