2010-02-21 4 views
3

Je ne sais pas comment gérer ces deux paginations distinctes dans la même vue. Merci pour ton aide.Cakephp deux paginations séparées du même modèle

Code 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); 

[...] 
+0

Veuillez lire ma réponse à une question similaire: http: // stackoverflow.com/a/21211278/2486198 – Mitja

Répondre

0

Comme l'aide Pagination repose sur le terrain $ controller- pagination> Je pense que cela est impossible. Un hack rapide serait de convertir une ou les deux paginations en celles basées sur Ajax. L'aide à la pagination fonctionne très bien avec l'assistant Ajax.

0

Est-ce une bonne idée d'avoir deux paginations distinctes sur une page? Il semble que vous essayez de paginer à travers des messages publics et des messages non publics - ce qui ne peut certainement pas être fait en même temps? C'est à dire. l'utilisateur ne sera jamais à la page 3 des publications publiques et à la page 2 des publications non publiques en même temps. Le seul cas où cela fonctionnerait vraiment est via AJAX avec la pagination en page comme suggéré par matiasf. Ne serait-il pas préférable d'afficher simplement les premiers x messages publics et non publics dans la vue actuelle et de créer une nouvelle vue pour afficher la pagination pour chaque accès par un lien, par exemple. "afficher tous les messages publics" et "afficher tous les posts non publics"?

2

Même si dans la plupart des cas, une bonne solution serait de rearchitect votre interface utilisateur de supprimer la nécessité pour le double pagination, ce qui suit est une solution de travail:

Tout d'abord, dans votre contrôleur vous substituez la fonction paginate() Cake à chercher la clé 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; 
    } 

Puis

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

utiliser ensuite comme si dans le contrôleur: $ this-> s et ('byJoinDate', $ this -> _ paginateBy ('random'));

Dans le modèle: echo $ paginator-> prev ('prev', array ('model' => $ par, 'class' => 'back'), null, array ('model' => $ par, 'class' => 'désactivé arrière'));

2

J'ai récemment dû faire ceci même parce que j'avais une seule page HTML qui avait des onglets dessus et dans chaque onglet il y avait une table paginée différente du même modèle avec des conditions différentes pour chacun.

La façon dont j'ai travaillé autour du problème était de créer des modèles fictifs dérivés du modèle que je voulais paginer plusieurs fois. Ensuite, j'ai simplement référencé ces modèles factices pour ma pagination.

Exemple:

Modèle de base

class Post extends appmodel { }; 

Modèles factices - il est important qu'ils utilisent la même table que le modèle de base

class Posts1 extends Post { var $useTable = 'posts'; } 
class Posts2 extends Post { var $useTable = 'posts'; } 

Dans votre contrôleur

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')); 
} 

Puis à votre avis

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

Grande solution de contournement –

Questions connexes