Cakephp 同型号的两个独立分页 [英] Cakephp two separate paginations of the same model
问题描述
我不知道如何在同一个视图中处理这两个单独的分页.感谢您的帮助.
I have no idea how to handle these two separate paginations in the same view. Thanks for helping.
控制器代码:
[...]
$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);
[...]
推荐答案
我最近不得不做这件事,因为我有一个 HTML 页面,上面有选项卡,并且每个选项卡中都有一个相同模型的不同分页表每个都有不同的条件.
I recently had to do this very thing because I had a single HTML page that had tabs on it and in each tab was a different paginated table of the same model with different conditions for each.
我解决这个问题的方法是创建虚拟模型,这些模型源自我想要多次分页的模型.然后我简单地引用了这些虚拟模型作为我的分页.
The way I worked around the problem was to create dummy models that derived from the model I wanted to paginate multiple times. Then I simply referenced those dummy models for my pagination.
示例:
基础模型
class Post extends appmodel { };
虚拟模型 - 重要的是它们使用与基本模型相同的表
Dummy Models - it is important that they use the same table as the base model
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(); ?>
这篇关于Cakephp 同型号的两个独立分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!