Cakephp 同型号的两个独立分页

标签 cakephp pagination

我不知道如何在同一个 View 中处理这两个单独的分页。谢谢你的帮助。

Controller 代码:

[...]
$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);

[...]

最佳答案

尽管在大多数情况下,正确的解决方案是重新构建您的 UI 以消除对双分页的需要,但以下是一个可行的解决方案:

首先,在您的 Controller 中覆盖 Cake 的 paginate() 函数以查找分页键:

    /**
     * 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);
     }

然后在 Controller 中像这样使用它:
$this->set('byJoinDate', $this->_paginateBy('random'));

在模型中:
echo $paginator->prev('prev', array('model' => $by, 'class' => 'back'), null, array('model' => $by, 'class' => 'disabled背部'));

关于Cakephp 同型号的两个独立分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2305839/

相关文章:

php - 在 PHP Core 中将现有字符串日期转换为 'm/j/Y'

php - 在 Cakephp 查找查询中转义特殊字符

php - jquery通过A$.post发送跨域数据

javascript - 是否可以在没有背景网格的情况下进行分页?

python - 使用 Python/Flask 在搜索结果中分页

php - CakePHP:什么应该进入/Lib vs/Vendor vs/Plugin

Bootstrap 中的 Cakephp ClassRegistry::init

javascript - Angular 分页不会对所有页面进行排序和过滤

ios - UIScrollView setContentOffset(Paging)被触摸屏停止

forms - 更新一些信息后,返回到特定分页页面 - Laravel 4