I have one web page and in this page I need total 2 paginations for different different 2 tables.
I have tried with different pagination but it's dependent with each other.
For ex., If I select second page of first table then second table automatically changed with 2nd page.
Here is my controller code code :
$page = $this->pageForPagination('User');
$this->paginate = array(
'User' => array(
'fields' => array(
'User.*',
),
'conditions' => $userConditions,
'page' => $page,
),
);
$this->set('users', $this->paginate('User'));
$page1 = $this->pageForPagination('Game');
$this->paginate = array(
'Game' => array(
'fields' => array(
'Game.*',
),
'conditions' => $conditions,
'page' => $page1,
),
);
$this->set('games', $this->paginate('Game'));
Here is the function that is used in above code :
function pageForPagination($model) {
$page = 1;
$sameModel = isset($this->params['named']['model']) && $this->params['named']['model'] == $model;
$pageInUrl = isset($this->params['named']['page']);
if ($sameModel && $pageInUrl) {
$page = $this->params['named']['page'];
}
$this->passedArgs['page'] = $page;
return $page;
}
Here is my view code :
echo $this->element('paging', array('model' => 'User'));
echo $this->element('paging', array('model' => 'Game'));
I have referred above code from this url : http://debuggable.com/posts/how-to-have-multiple-paginated-widgets-on-the-same-page-with-cakephp:48ad241e-b018-4532-a748-0ec74834cda3
Can any one help me because I didn't get any solution still?
The solution in the link you mentioned is to override the default pagination urls and doesn't display both models at the same time. You haven't implemented the solution completely.
You have to extract the page number manually for each model like this:
function pageForPagination($model) {
if (isset($this->params['named']['pagefor'.$model])
return $this->params['named']['pagefor'.$model];
else
return 1;
}
The code below:
echo $this->element('paging', array('model' => 'User'));
echo $this->element('paging', array('model' => 'Game'));
causes the cakephp to use the default pagination view and the urls in the default pagination are the same for both models. You have to use custom pagination view and specify the model for the page link.
For the edited function above you can use a url pattern like this:
/Controller/Action/pageforUser:215/pageforGame:35
for each page link in your view.
I think best practice to use this url pattern is to override the pagination helper so that the links that are created form the following pattern:
'/Controller/Action/pagefor[Model]:215'.[other parameters]
Related
I am using pagination on my CakePHP page and it works well, but I have a few pages generated by different functions in one controller file and there is an cumbersome issue.
Let's assume I've got two tables with hundreds of entries, which my paginators divide and display 15 of entries on one page:
public $paginate = array(
'Table1' => Array(
'order' => array('Table1.id' => 'desc'),
'limit' => 15
),
'Table2' => Array(
'order' => array('Table2.id' => 'asc'),
'limit' => 15
)
);
And functions in this controller file:
public function showTable1() {
$this->set('table', $this->paginate('Table1'));
}
public function showTable2() {
$this->set('table', $this->paginate('Table2));
}
When I go to the next page while displaying "Table 1", I go to the next page and it works well. But when I open another page with "Table 2" generated by another function from the same controller file, it unfortunately also goes to the second page.
What should I do to have different "pages indicators"? I mean, when I go to the third page on "Table 1", I don't want to go to the third page when I go to "Table 2".
Thanks in advance. I hope I presented my problem clearly.
instead of setting the pagination options on the top of the controller, try deleting that public $paginate and setting all the options inside action, like this (and in the similar way for the second action)
public function showTable1() {
$options = array(
'conditions' => array(
'Table1.id' => whatever
),
'fields' => array(
'Table1.id'
),
'order' => array(
'Table1.id' => 'DESC'
),
'limit' => 15
);
$this->Paginator->settings = $options;
$table = $this->Paginator->paginate('Table1');
$this->set(compact('table'));
}
I'm using CakePHP v2.4.1, and I'm trying to have two separate models be paginated on the same page, through the same controller action (index)
I want to have a NewsPost paginated on a single page, and the EventPost also paginated separately on the same page. Is this bad practice, or is it possible to set conditions to change the $this->paginate variable to paginate properly depending on which model it is on?
My Controller looks like this:
public $uses = array(
'NewsPost',
'NewsPostComment',
'EventPost',
'EventPostComment'
);
public $paginate = array(
'limit' => 9,
'order' => array(
'NewsPost.created' => 'desc'
),
'recursive' => 1,
);
public function index() {
$this->paginate = array(
'limit' => 1,
'order' => array(
'EventPost.created' => 'desc'
),
'recursive' => 1,
);
$this->Paginator->settings = $this->paginate;
$newsPosts = $this->paginate('NewsPost');
$eventPosts = $this->EventPost->find('all');
$this->set(array('newsPosts' => $newsPosts, 'eventPosts' => $eventPosts));
The built-in cake paginator does not seem to allow for the options of paginating separate models, or am I wrong?
Here is the view for pagination:
<?php
$params = $this->Paginator->params();
if ($params['count'] > 0): ?>
<div class="pagination-totals pull-left">
<?php echo $this->Paginator->counter(array('format' => __('{:start} to {:end} of {:count}'))) ?>
</div>
<?php endif; ?>
<?php echo $this->Paginator->numbers(array(
'class' => 'pull-right',
'prev' => '<',
'next' => '>',
));
?>
The problem is, when I choose page two for the NewsPost, it switches to page two for the EventPost, so how can I differentiate between both separate models on the same page using CakePHP pagination?
Thanks
You have just one params named page in your page so the result in normal.
You have to handle this with ajax request and separated actions
i need to create search method with pos data :
search method :
public function find_estate () {
if(isset($this->request->data['type'])){
$type =$this->request->data['type'] ;
}else{$type="";}
if($type!=""){
if ($type != "all"){
$where[] = "Estate.melk_type LIKE '$type'";
}
}
}
if(isset($where)){
$this->paginate =
array(
'Estate'=>
array(
'limit' => 4,
'order' => array(
'Estate.eid' => 'desc'
),
'fields' => array('eid','melk_type','status ','image'),
'conditions' => implode(" AND ",$where),
));
$this->set('estate',$this->paginate());
}
}
but not working in other page of search result
How do I paging with post data ?
You need to send $type param when you're switching pages, also I would use get method if it's a simple search with one field, from what I see you don't test for the method so you could easily add search param to the URLs in pagination
here's another question about this, you can also try official documentation
I have a model association that works as followed:
Users hasAndBelongsToMany Tags
I want to be able to paginate a group of users based on their tags. Users can have many tags and filter requested can be complex i.e. a query can ask for all the users with tags A and B but not C. Its way more manageable to handle this by using pagination on tags (so that the condiditions array can apply to the Tag model) and getting back the users that are within that tags subset.
The issue then comes when trying to paginate, because I set up the pagination as followed:
$this->paginate = array(
'conditions' => $conditions,
'contain' => array('User'),
'limit' => 5,
);
$this->paginate('Tag')
So then pagination does exactly what is expected, it returns the array for pagination based on tags, but what I want is to paginate by the users. Switching the pagination to hinge on the users creates its own set of problems ('that is what I was using before'). Just pagination exist for model relates like this? I feel like this is the type of thing cakephp might be able to do. Thank you to anyone who helps.
Place this into the model.
function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$parameters = compact('conditions');
$this->recursive = $recursive;
$count = $this->find('count', array_merge($parameters, $extra));
if (isset($extra['group'])) {
$count = $this->getAffectedRows();
}
return $count;
}
You should use custom finder: cakephp documentation link. In you example in Users Model create protected function:
protected function _findUsersByTag($state, $query, $results = array()) {
if($state == 'before') {
$query['joins'] = array(
'Tag' => array('type' => 'inner', 'alias' => 'Tag', 'table' => 'tags', 'conditions' => array('Users.tag_id = Tag.id')
);
return $query;
}
return $results;
}
Second add in Users Model:
public $findMethods = array('usersByTag' => true);
And in the controller you definie you pagination to use custom finder using 0 index of array or in 2.3 and higther cake version property called findType:
public $pagination = array('User' => array(
'usersByTag',
'limit' => 5,
'conditions' => $conditions
));
I have an action called index and another one called manage, both in PostsController. I want to implement pagination for both, and I've set up this class attribute:
public $paginate = array(
'limit' => 10,
'order' => array(
'Post.created' => 'desc'
)
);
then I'm using the pagination in my index action like so: $this->set('posts', $this->paginate('Post'));
This results in a URL like so: http://dev/posts/page:2 which is fine.
However, when I try to use pagination in my manage action just like I did with index ($this->set('posts', $this->paginate('Post'));), the pagination links on my view redirect to the URL above, rather than the manage action.
Basically, Cake is getting confused because I'm using pagination twice in the same controller and it's redirecting both to the same URL. How can I make sure that the pagination for the manage action works properly?
Do the followings.
<?php
class PostsController extends AppController
{
var $name = 'Posts';
public $paginate = array
(
'limit' => 10,
'order' => array
(
'Post.created' => 'desc'
)
);
function index()
{
$this->Post->recursive = 0;
$this->set('posts', $this->paginate('Post'));
}
function manage()
{
$this->Post->recursive = 0;
$this->set('posts', $this->paginate('Post'));
}
}
make changes in view file as defined below.
index.ctp
$options = array
(
'url'=> array
(
'controller' => 'posts',
'action' => 'index'
)
);
$paginator->options($options);
manage.ctp
$options = array
(
'url'=> array
(
'controller' => 'posts',
'action' => 'manage'
)
);
$paginator->options($options);
And you are done.