Ajax-pagination triggers only once - cakephp

i implemented the pagination-components according to the official documentation:
http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
I also included ajax-support. Basically everthing works fine except one thing: If i switch from page 1 to page 2 the ajax-request will work, after that i switch from page 2 to 3 instead a ajax-request a normal Post-Request will be performed. So the ajax-thing works only once.
My controller looks like this (short form):
public $components = array('Paginator', 'RequestHandler');
public $helpers = array('Js', 'Categorie');
....
$this->Paginator->settings = array(
'limit' => '15'
);
No the more important thing is my view: (jquery are included in my layout...)
$this->Js->JqueryEngine->jQueryObject = 'jQuery';
$this->Paginator->options(array(
'update' => '#paginate',
'evalScripts' => true,
'before' => $this->Js->get('#busy-indicator')->effect('fadeIn', array('buffer' => false)),
'complete' => $this->Js->get('#busy-indicator')->effect('fadeOut', array('buffer' => false))
));
?>
<?php
echo $this->Html->image('indicator.gif', array(
'id' => 'busy-indicator'
));
?>
<div id="paginate">...display data...</div>
<?php
echo $this->Paginator->numbers();
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
echo $this->Paginator->counter();
?>
That's all... as mentioned before: Switch Page 1->2 will work, Switch Page 2-3 will do a normal POST-Request, Switch 3-4 will work and so one...
A bit strange, so any idea? Btw. i tried different jquery-versions without any effect (currently i am using 2.0.3...)
Thanks - Best regards
Teyhouse

In this case, you just needed to also have:
<?php echo $this->Js->writeBuffer(); ?>
In your app/View/Layouts/ajax.ctp file.

Related

How to implement pagination with search in CakePHP

My pagination is working, even the search is also working. But the problem that I'm having is when i click on next page link in the pagination links. The search is not working for the next page of the pagination. Also I need to know how I send other parameters through the url, and use them in query of pagination. I need help on this as I am novice in CakePHP.
In controller page I have used this code:
class StatesController extends AppController {
public $components = array('Paginator');
public $paginate = array(
'limit' => 2,
'fields' => array('State.id', 'State.state','State.code'),
'order' => array(
'State.state' => 'asc'
)
);
public function admin_index() {
$this->layout = false;
$this->layout = 'adminlayout';
//****** pagination starts
$search=$this->request->data('State.search');
$this->Paginator->settings = $this->paginate;
// similar to findAll(), but fetches paged results
$stateListAr = $this->Paginator->paginate('State',
array('State.state LIKE' => "%".$search."%")
);
$this->set('stateListAr', $stateListAr);
//****** pagination ends
$this->set('stateListAr',$stateListAr);
$this->render('admin_index');
}
}
In view page I have used this code:
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(array('first' => 'First page')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
i have got solution to my problem , here it is . I have used a code in view page , here $search variable i have setted data from controller.
$search);
$this->Paginator->options(array(
'url' => $urlParamAr
));
echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo $this->Paginator->numbers(array('first' => 'First page'));
echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
?>

How to customize CakePHP pagination url?

currently i am using cakephp version 2.5.3
I want to change my cakephp pagination url.
My current URL is http://www.example.com/newsFeeds/ajax_news_feed/page:2 I need http://www.example.com/newsFeeds/index/page:2
My Code:
<?php
echo $this->Paginator->prev(' <<' . __('Previous '), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers();
//echo $this->Paginator->url(array('controller'=>'newsFeeds', 'action'=>'index'));
//echo $this->Paginator->link('Sort by title on page 5', array('controller'=>'newsFeeds', 'action'=>'index'));
echo $this->Paginator->next(__(' Next') . '>> ', array(), null, array('class' => 'next disabled'));
?>
Above pagination is showing-
When i am clicking 2 then the link is going to http://www.example.com/newsFeeds/ajax_news_feed/my_post/page:2 but i need http://www.example.com/newsFeeds/index/my_post/page:2
Please tell me how to change controller and action in pagination?
User $this->Paginator->options-
Code:
<?php
$this->Paginator->options['url'] = array('controller' => 'newsFeeds', 'action' => 'index/my_post');
echo $this->Paginator->prev(' <<' . __('Previous '), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers();
echo $this->Paginator->next(__(' Next') . '>> ', array(), null, array('class' => 'next disabled'));
?>
For cakephp 3 its a little bit different:
$this->Paginator->options([
'url' => [
'controller' => 'newsFeeds',
'action' => 'index',
'my_post']
]);

How to do multiple pagination without page not found error in Cakephp

I have paginate 2 models (Income,Expanse) in the Student model view page .
I have really page not found problem with cakephp's pagination.
It have no any problem when the paginate result has only 1 page or the same, but It will error if one has more paginate result than other.
for example.
-income has paginate result 1 page and expanse has 1 page. No problem at all.
-income has 10 pages and expanse has 10 pages no problem at all.
-income has 2 pages and expanse has 1 page. income page 2 page not found error.
-income has 5 pages and expanse has 2 pages. income page 3,4,5 page not found error.
-income has 10 pages and expanse has 13 pages. expanse page 11,12,13 page not found error.
for example(not real one) , Student's view have income and expense items ,Both are display as pagination.
//this is how I config paginator in Student controller.
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'recursive'=>0,
)
);
<div class="paging">//this is how I config Income paginator in Student view
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Income'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Income','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Income'), null, array('class' => 'next disabled'));
?>
</div>
//this is how I config Expanse paginator in Student view
<div class="paging">
<?php
echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Expanse'), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('model'=>'Expanse','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
echo $this->Paginator->next(__('next') . ' >', array('model'=>'Expanse'), null, array('class' => 'next disabled'));
?>
</div>
Please help me. sorry for my english
If you have any question , please ask me.
Thank you.
Yes It is possible.....
Step1 : Make paging.ctp in View/Elements/paging.ctp
<?php if (!isset($this->Paginator->params['paging'])) {
return;
}
if (!isset($model) || $this->Paginator->params['paging'][$model]['pageCount'] < 2) {
return;
}
if (!isset($options)) {
$options = array();
}
$options['model'] = $model;
$options['url']['model'] = $model;
$this->Paginator->__defaultModel = $model;
?>
<div class="paging">
<ul class="pagination pull-right">
<?php
echo $this->Paginator->prev('<<', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
echo $this->Paginator->numbers(am($options,array('tag' => 'li', 'separator' => '', 'currentClass' => 'active', 'currentTag' => 'a')));
echo $this->Paginator->next('>>', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
?>
</ul>
</div>
Step2: Now add this function to Controllers/AppController
public function pageForPagination($model) {
$page = 1;
$sameModel = isset($this->request->params['named']['model']) && $this->request->params['named']['model'] == $model;
$pageInUrl = isset($this->request->params['named']['page']);
if ($sameModel && $pageInUrl) {
$page = $this->request->params['named']['page'];
}
$this->passedArgs['page'] = $page;
return $page;
}
Step3: Now in controller action do some conditions so that proper page should be call
if (empty($this->request->params['named'])) {
$page = $this->pageForPagination('Income');
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
),
'Expense' => array (
'order'=>array('expense_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0,
));
} else if ($this->request->params['named']['model'] == 'Income') {
$page = $this->pageForPagination('Income');
public $paginate = array(
'Income' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page,
'recursive'=>0
));
} else if ($this->request->params['named']['model'] == 'Expense') {
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
'Expense' => array (
'order'=>array('income_id'=>'ASC'),
'limit'=>10,
'page' => $page1,
'recursive'=>0
));
Step 4: Now call the paging element in your student view
<?php echo $this->element('paging', array('model' => 'Income'));?>
<?php echo $this->element('paging', array('model' => 'Expense'));?>
Note: Please take care of brackets and semicolon.......and sorry to be late but will help others...Thanks..
I think if you use two different iframes to load different URLs for pagination, you can handle this.
OR you have to call ajax functions to paginate 2 models in one page without reloading the page....

cakephp pagination next and previous not showing

I am using cakephp 1.3 and i am receiving strange problem with pagination.I have imnplemented pagination for I have working pagination code in more than one controller .But my rest of the controller not showing next and previous button and numbers for pagination is working fine
I am using this code in my view
<?php
if(!empty($zipcodes)){
echo "<div class='pagination'>";
echo #$this->Paginator->prev('« Previous', null, null, array('class' => 'disabled'));
echo #$this->Paginator->numbers();
echo #$this->Paginator->next('Next »', null, null, array('class' => 'disabled'));
echo '<div style="float:right;padding:5px;color:#000">'.$this->Paginator->counter().'</div>';
echo "<div class='clear'></div>";
echo "</div>";
}
?>
And in my controller i have used pagination controller as i am paginating custom query results .I included pagination code in app_model.php.
My ontroller code is
var $paginate = array(
'Zipcode' => array('limit' => 5,
'order' => array('id' => 'desc'),
)
);
my query and operation
$tsql = " SELECT Zipcode.* ".
" FROM zipcodes AS Zipcode ".
" WHERE Zipcode.region_id=0 ";
$conditions = array(
'tsql'=>$tsql,
);
$tmp = $this->paginate('Zipcode',$conditions);
$this->set('zipcodes', $tmp);
can any one point out me what i am doing wrong.??And why pagination is working for only some controller .??? THanks in advance
First, change your paginate in the top of the controller. Remove Zipcode from it. It should just be:
var $paginate = array(
'limit' => 5,
'order' => array('Zipcode.id' => 'desc'),
);
Second, I am not sure what you are trying to accomplish with $tsql, but as far as I know, you cannot pass a SELECT statement as a condition. You need to write ORM specific conditions like this:
$this->paginate = array(
'conditions' => array('Zipcode.region_id' => 0),
);
Then to set zipcodes you can do this:
$this->set('zipcodes', $this->paginate('Zipcode'));
or this
$data = $this->paginate('Zipcode');
$this->set(compact('data'));
As a side note, echo #$this->Paginator->prev should be echo $this->Paginator->prev and all the # should be removed from those lines. That is poor coding practice. I'm not certain if you have the ignore in there for a reason, but it's bad.

cakephp: how to render related models in the view

My objective is to display records for a related child model (once removed) in the view. My problem is that I receive a 'Notice (8): Undefined index:' error message when I try to output the following query.
Tournaments have many tournamentDetails, and tournamentDetails have many updates. I'm trying to display all the updates for each tournamentDetail that is displayed on the tournaments view.
So my question is how to properly resolve the error message and display the updates for each tournamentDetail on the tournaments view page.
My find data in the 'tournaments' controller view action is:
$updates = $this->Tournament->TournamentDetail->Update->find('all', array( 'tournament_details.tournament_id'=> $this->data['Tournament']['id']));
In the view, I have this code.
<?php foreach ($tournament['Update'] as $update): ?>
<h3>Updates</h3>
<h1><?php echo $update ['title']; ?></h1>
<p><?php echo $update ['body']; ?></p>
<hr/>
<?php endforeach; ?>
This is the same 'foreach' code the I use for the other related child records without problem. I did use the debug() function to investigate but didn't see what I was missing.
The output of the $updates array from the debug function looks like this:
Array
(
[0] => Array
(
[Update] => Array
(
[id] => 1
[title] => first round matches start today
[date] => 2010-03-19
[body] => this tournament's first round matches start today.
[tournament_detail_id] => 4
[homeStatus] => no
)
Is there a special way to display records this deep?
As always, thanks in advance.
Paul
Update: Feb 23/11
After some testing the problem I seem to have is finding and passing the correct value to the $updates variable;
To summarize, I want the $updates variable to hold the current 'tournament_details_id'. This will then display the related update records in the 'tournaments' view.
I'm still a beginner and most likely overlooked the obvious.
Here is the model info:
class Tournament extends AppModel {
var $name = 'Tournament';
var $hasMany = array(
'TournamentDetail' => array(
'className' => 'TournamentDetail',
'foreignKey' => 'tournament_id',)
class TournamentDetail extends AppModel {
var $name = 'TournamentDetail';
var $belongsTo = array(
'Tournament' => array(
'className' => 'Tournament',
'foreignKey' => 'tournament_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
class Update extends AppModel {
var $name = 'Update';
var $belongsTo = array(
'TournamentDetails' => array(
'className' => 'TournamentDetails',
'foreignKey' => 'tournament_detail_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Controller data:
class TournamentsController extends AppController
function view($slug = null) {
if (!$slug) {
$this->Session->setFlash(__('Invalid Tournament.', true));
$this->redirect(array('action'=>'index'));
}
$this->Tournament->recursive = 1;
$tournament = $this->Tournament->findBySlug($slug);
$updates = $this->Tournament->TournamentDetail->Update->find('all', array('conditions' => array( 'tournament_details_id' => $this->data['TournamentDetails']['id'] )));
$this->set(compact('tournament','updates', $updates ));
Tournament view. This is display the 'tournament details' and (ideally) related tournament detail 'updates'
<h2><?php echo $tournament['Tournament']['name']; ?></h2>
<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
<h1><?php echo $tournamentDetail ['title']; ?></h1>
<p><?php echo $tournamentDetail ['body']; ?></p>
<?php endforeach; ?>
<?php foreach ($updates['Update'] as $update): ?>
<h4>Update: <?php echo $update ['date']; ?></h4>
<p> <?php echo $update ['Update'] ['title']; ?></p>
<p><?php echo $update ['Update'] ['body']; ?></p>
<?php endforeach; ?>
I've tested this by adding in the tournament details 'id' as an integer and it pulls up the correct 'update' record. However I seem to have problems configuring the find operation to do the same.
As always I appreciate the help.
Thanks
Paul
Based on your debug output, it seems that your $tournaments variable consists of an array (instead of the associative array) so you might want to use the foreach to access each of those elements:
foreach ($tournaments as $update):
?>
<h3>Updates</h3>
<h1><?php echo $update ['Update']['title']; ?></h1>
<p><?php echo $update ['Update']['body']; ?></p>
<hr/>
<?php endforeach; ?>
It appears that you be using the wrong variable, you talk about the $updates variable but you're using the $tournament variable in your view.
<?php foreach ($updates['Update'] as $update): ?>
<h3>Updates</h3>
<h1><?php echo $update ['title']; ?></h1>
<p><?php echo $update ['body']; ?></p>
<hr/>
<?php endforeach; ?>
Additionally you haven't posted the whole controller method, which includes that find but you might not have sent the data to the view yet either
$this->set(compact('updates'));

Resources