Cakephp 和分页多个结果集 [英] Cakephp and paginating multiple result sets

查看:27
本文介绍了Cakephp 和分页多个结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 $this->paginate 在一个动作中被调用一次时,我的分页工作正常,但它似乎不能很好地支持多次调用.这就是我想要做的......

function admin_index() {$published = $this->pagination('Post', array('status'=>1));$unpublished = $this->pagination('Post', array('status'=>0));$this->set('published', $published);$this->set('unpublished', $unpublished);}

我的观点的简化版本是:

未发表的帖子

<?php echo $this->element('table', array('posts'=>$unpublished));?><h1>已发表的帖子</h1><?php echo $this->element('table', array('posts'=>$published));?>

简单,我可以重复使用相同的元素来显示不同类型的数据.除了 jquery 表排序器/对 Cakephp 的 hacks 之外,真的没有办法单独对这些表中的每一个进行分页吗?

我知道我可以将所有数据放在一个表中,然后只使用排序来区分状态,但我想先检查是否遗漏了什么.

解决方案

由于这方面的活动不多,我继续做了合理的事情并想出了一个解决方法.以下是我想出的:

控制器:

function index() {$active = $this->paginate('Post', array('Post.status'=>1));$activePaging = $this->request->params['paging']['Post'];$inactive = $this->paginate('Post', array('Post.status'=>0));$inActivePaging = $this->request->params['paging']['Post'];$this->set('active', $active);$this->set('inactive', $inactive);$this->request->params['paging']['Active'] = $activePaging;$this->request->params['paging']['InActive'] = $inActivePaging;unset($this->request->params['paging']['Post']);}公共功能分页(){如果 ($this->request->is('ajax')) {$model = $this->request->params['named']['model'];$status = $model === '活动' ?1:0;$posts = $this->paginate('Post', array('Post.status'=>$status));$paging = $this->request->params['paging']['Post'];unset($this->request->params['paging']['Post']);$this->request->params['paging'][$model] = $paging;$this->set('posts', $posts);$this->set('model', $model);$this->render('/Posts/ajax_posts_paginated', 'ajax');}}

视图:(简化)

index.ctp

非活动帖子

<div class="well" id="InActive"><?php echo $this->element('posts_table_paginated', array('posts'=>$inactive, 'model'=>'InActive'));?>

<h3>活跃帖子</h3><div class="well" id="Active"><?php echo $this->element('posts_table_paginated', array('posts'=>$active, 'model'=>'Active'));?>

elements/posts_table_paginated.ctp

<?php$this->Paginator->options(array('更新' =>'#'.$模型,'evalScripts' =>真的,'网址'=>数组('控制器'=>'帖子','动作'=>'分页','模型'=>$模型)));?><表格><tr><th><?php echo $this->Paginator->sort('id', 'ID', array('model'=>$model));?></th><th><?php echo $this->Paginator->sort('title', 'Title', array('model'=>$model));?></th><th><?php echo $this->Paginator->sort('created', 'Created', array('model'=>$model));?></th><th>由</th>创建</tr><?phpforeach($posts 作为 $post):?><tr><td><?php echo $post['Post']['id'];?></td><td><?php echo $post['Post']['title'];?></td><td><?php echo $post['Post']['created'];?></td><td><?php echo $post['User']['username'];?></td></tr><?phpEndforeach;?><?php如果 ($this->params['paging'][$model]['count'] > 0) {?><div id="分页"><ul id="分页"><?phpecho $this->Paginator->prev(__('previous'), array('tag' => 'li', 'model'=>$model), null, array('tag'=>;'li', 'class'='=>'prev off'));echo $this->Paginator->numbers(array('tag'=>'li', 'model'=>$model, 'separator' => false));echo $this->Paginator->next(__('next'), array('tag' => 'li', 'model'=>$model), null, array('tag'=>;'li', 'class'=>'next off'));?>

<?php}echo $this->Js->writeBuffer();?>

I have pagination working fine for when $this->paginate is called once in an action, but it does not appear to be very good support for multiple calls. Here is what I'm looking to do ...

function admin_index() {

    $published = $this->pagination('Post', array('status'=>1));
    $unpublished = $this->pagination('Post', array('status'=>0));

    $this->set('published', $published);
    $this->set('unpublished', $unpublished);
}

A simplified version of my view would be:

<h1>Unpublished Posts</h1>
<?php echo $this->element('table', array('posts'=>$unpublished)); ?>
<h1>Published Posts</h1>
<?php echo $this->element('table', array('posts'=>$published)); ?>

Simple, and I can re-use the same element to display different types of data. Is there really no way around paginating each of these table separately other than jquery table sorter / hacks to Cakephp?

I understand that I could stick all the data in one table and then just use sorting to differentiate between the statuses, but I wanted to check if I was missing something first.

解决方案

Since there was not much activity on this, I went ahead and did the reasonable thing and figured out a workaround. Below is what I came up with:

Controller:

function index() {
    $active = $this->paginate('Post', array('Post.status'=>1));     
    $activePaging = $this->request->params['paging']['Post'];

    $inactive = $this->paginate('Post', array('Post.status'=>0));
    $inActivePaging = $this->request->params['paging']['Post'];

    $this->set('active', $active);
    $this->set('inactive', $inactive);

    $this->request->params['paging']['Active'] = $activePaging;
    $this->request->params['paging']['InActive'] = $inActivePaging;
    unset($this->request->params['paging']['Post']);
}

public function paginate() {
    if ($this->request->is('ajax')) {
        $model = $this->request->params['named']['model'];
        $status = $model === 'Active' ? 1 : 0;

        $posts = $this->paginate('Post', array('Post.status'=>$status));
        $paging = $this->request->params['paging']['Post'];
        unset($this->request->params['paging']['Post']);
        $this->request->params['paging'][$model] = $paging;

        $this->set('posts', $posts);
        $this->set('model', $model);
        $this->render('/Posts/ajax_posts_paginated', 'ajax');
    }
}

Views: (simplified)

index.ctp

<h3>Inactive Posts</h3>
<div class="well" id="InActive">
<?php echo $this->element('posts_table_paginated', array('posts'=>$inactive, 'model'=>'InActive')); ?>
</div>

<h3>Active Posts</h3>
<div class="well" id="Active">
<?php echo $this->element('posts_table_paginated', array('posts'=>$active, 'model'=>'Active')); ?>
</div>

elements/posts_table_paginated.ctp

<?php
$this->Paginator->options(array(
    'update' => '#'.$model,
    'evalScripts' => true,
    'url'=> array('controller'=>'posts', 'action'=>'paginate', 'model'=>$model)
));
?>
<table>
    <tr>
        <th><?php echo $this->Paginator->sort('id', 'ID', array('model'=>$model)); ?></th>
        <th><?php echo $this->Paginator->sort('title', 'Title', array('model'=>$model)); ?></th>
        <th><?php echo $this->Paginator->sort('created', 'Created', array('model'=>$model)); ?></th>
        <th>Created by</th>
    </tr>
    <?php 
    foreach ($posts as $post): 
    ?>
    <tr>
        <td><?php echo $post['Post']['id']; ?></td>
        <td><?php echo $post['Post']['title']; ?></td>
        <td><?php echo $post['Post']['created']; ?></td>
        <td><?php echo $post['User']['username']; ?></td>
    </tr>
    <?php 
    endforeach; 
    ?>
</table>
<?php
    if ($this->params['paging'][$model]['count'] > 0) {
?>
<div id="pagination">
    <ul id="paginate">
        <?php
            echo $this->Paginator->prev(__('previous'), array('tag' => 'li', 'model'=>$model), null, array('tag'=>'li', 'class'=>'prev off'));
            echo $this->Paginator->numbers(array('tag'=>'li', 'model'=>$model, 'separator' => false));
            echo $this->Paginator->next(__('next'), array('tag' => 'li', 'model'=>$model), null, array('tag'=>'li', 'class'=>'next off'));
        ?>
    </ul>
</div>

<?php 
    }
    echo $this->Js->writeBuffer(); 
?>

这篇关于Cakephp 和分页多个结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆