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

问题描述

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

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

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

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:

控制器:

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

观看次数:(简体)

index.ctp

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

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和分页多个结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 14:34