我不知道如何在同一视图中处理这两个单独的分页。谢谢你的帮助。
控制器代码:
[...]
$this->Post->bindModel(array(
'hasAndBelongsToMany'=>array('Tag'),
'belongsTo'=>array('User')),false);
if($this->Session->check('Auth.User.id'))
$this->Post->bindModel(array(
'hasMany'=>array(
'UserVote'=>array('conditions'=>array('UserVote.user_id'=>$this->Session->read('Auth.User.id') )),
'Favorite'=>array('conditions'=>array('Favorite.user_id'=>$this->Session->read('Auth.User.id') ))
)), false);
$posts = $this->paginate($this->Post,array('Post.public'=>1,'Post.user_id'=>$uid));
$posts2 = $this->paginate($this->Post,array('Post.public'=>0,'Post.user_id'=>$uid));
$this->set('posts',$posts);
$this->set('posts2',$posts2);
[...]
答案 0 :(得分:2)
即使在大多数情况下,正确的解决方案是重新构建您的UI以消除双重分页的需要,以下是一个可行的解决方案:
首先,在你的控制器中你覆盖Cake的paginate()函数来寻找paginator键:
/**
* Handles automatic pagination of model records.
*
* @param mixed $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
* @param mixed $scope Conditions to use while paginating
* @param array $whitelist List of allowed options for paging
* @return array Model query results
* @access public
* @link http://book.cakephp.org/view/165/Controller-Setup
*/
function paginate($object = null, $scope = array(), $whitelist = array(), $key = null) {
$results = parent::paginate($object, $scope, $whitelist);
if ($key) {
$this->params['paging'][$key] = $this->params['paging'][$object];
unset($this->params['paging'][$object]);
}
return $results;
}
然后
/**
* undocumented function
*
* @param string $key
* @return void
* @access public
*/
function _pageForPagination($by) {
$page = 1;
$samekey = isset($this->params['named']['by']) && $this->params['named']['by'] == $by;
$pageInUrl = isset($this->params['named']['page']);
if ($samekey && $pageInUrl) {
$page = $this->params['named']['page'];
}
$this->passedArgs['page'] = $page;
return $page;
}
/**
* FIXME: Wrapper for Cake's pagination
* Change pagination criteria on the fly (conditions, grouping, order, limit)
*
* @param string $model
* @param string $criteria
* @return void
* @author Andrew
*/
function _paginateBy($key) {
$this->User->unbindModel(array('hasMany' => array('UserImage')), false);
$this->paginate['User'] = am($this->User->getCriteria($key), array('page' => $this->_pageForPagination($key)));
return $this->paginate('User', array(), array(), $key);
}
然后在控制器中使用它: $ this-> set('byJoinDate',$ this-> _paginateBy('random'));
在模型中: echo $ paginator-> prev('prev',array('model'=> $ by,'class'=>'back'),null,array('model'=> $ by,'class' =>'禁用后退'));
答案 1 :(得分:2)
我最近不得不这样做,因为我有一个HTML页面,其上有标签,每个标签中都有一个不同的分页表,每个表格都有不同的条件。
我解决这个问题的方法是创建从我想要多次分页的模型派生的虚拟模型。然后我简单地为我的分页引用了那些虚拟模型。
示例:
基础模型
class Post extends appmodel { };
虚拟模型 - 重要的是他们使用与基本模型相同的表
class Posts1 extends Post { var $useTable = 'posts'; }
class Posts2 extends Post { var $useTable = 'posts'; }
在您的控制器中
function multiview($id = null) {
$this->paginate['Posts1'] = array(
'conditions'=>array('Posts1.field'=>0),
'limit'=>5
);
$this->set('posts1', $this->paginate('Posts1'));
$this->paginate['Posts2'] = array(
'conditions'=>array('Posts2.field'=>1),
'limit'=>5
);
$this->set('posts2', $this->paginate('Posts2'));
}
然后在你看来
Display first paginated data
<?php foreach ($posts1 as $post): ?>
Do Paginated row display here...
<?php endforeach; ?>
<!-- Shows the page numbers -->
<?php echo $this->Paginator->numbers(array('model'=>'Posts1')); ?>
<!-- Shows the next and previous links -->
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
<!-- prints X of Y, where X is current page and Y is number of pages -->
<?php echo $this->Paginator->counter(); ?>
Display second paginated data
<?php foreach ($posts2 as $post): ?>
Do Paginated row display here...
<?php endforeach; ?>
<!-- Shows the page numbers -->
<?php echo $this->Paginator->numbers(array('model'=>'Posts2')); ?>
<!-- Shows the next and previous links -->
<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?>
<!-- prints X of Y, where X is current page and Y is number of pages -->
<?php echo $this->Paginator->counter(); ?>
答案 2 :(得分:0)
由于Pagination助手依赖于$ controller-&gt;分页字段,我认为这是不可能的。快速破解就是将一个或两个分页转换为基于Ajax的分页。分页助手与Ajax助手配合得很好。
答案 3 :(得分:0)
在页面上有两个单独的分页是一个好主意吗?看起来你正试图通过公共帖子和非公开帖子进行分页 - 这肯定不能同时完成?即用户不会同时出现在公共帖子的第3页和非公开帖子的第2页。唯一可行的情况是通过AJAX,页面中的分页会出现在matiasf的建议中。
在当前视图中显示前x个公开和非公开帖子并创建新视图以显示链接访问的每个帖子的分页,这不是更好的解决方案吗“查看所有公开信息”和“查看所有非公开信息”?