2010-02-21 68 views
3

的獨立paginations我不知道如何在同樣的觀點來處理這兩個單獨paginations。感謝您的幫助。CakePHP的兩個同型號

控制器代碼:

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

請閱讀我的答案類似的問題:http:// stackoverflow。com/a/21211278/2486198 – Mitja 2014-12-07 18:21:33

回答

0

由於分頁助手依賴於$控制器 - >分頁領域,我認爲這是不可能的。快速入侵將是將一個或兩個分頁轉換爲基於Ajax的分頁。分頁幫助器與Ajax幫手很好地協作。

0

是它有一個頁面上有兩個獨立的paginations一個好主意?它看起來像你試圖通過公共帖子和非公開帖子分頁 - 這肯定不能在同一時間完成?即用戶不會同時在公共帖子的第3頁和非公共帖子的第2頁。這是真正有效的唯一情況,就是通過AJAX在頁面中按照matiasf的建議進行分頁。

這豈不是一個更好的解決方案,只是顯示在當前視圖中的第一個x公共和非公開的信息,並創建一個新的視圖來顯示每一個鏈接訪問分頁例如「查看所有公開帖子」和「查看所有非公開帖子」?

2

即使在大多數情況下,正確的解決辦法是重新構建你的UI,除去雙分頁的需要,下面是一個有效的解決方案:

首先,在你的控制器,你覆蓋蛋糕的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); 
    } 

然後使用它像這樣的控制器: $這個 - >取值et('byJoinDate',$ this - > _ paginateBy('random')); ('model'=> $ by,'class'=>'back'),null,array('model'=> $();}返回頁首返回頁首返回頁首返回頁首返回頁首返回頁首','class'=>'disabled back'));

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(); ?> 
+0

偉大的解決方法 – 2014-02-19 21:56:54