【问题标题】:Sortable column headers without pagination?没有分页的可排序列标题?
【发布时间】:2012-10-28 21:09:35
【问题描述】:

我希望能够在不需要分页的情况下显示所有记录,但仍希望通过使用 $this->Paginator->sort() 之类的东西来保留具有可排序列标题的功能。

目前我正在设置'limit' => 100,因为我知道我一次不会有超过 50 条记录,这很好用,但是我想知道是否有基本的方法来完成可排序没有分页的列以其他方式,例如 'limit' => unlimited 或其他可以实现此目的的 CakePHP 组件。

这是控制器:

class GroupsController extends AppController {

    public function view($slug = null) {

        $group = $this->Group->findBySlug($slug);

        $this->paginate = array(
            'Person' => array(
                'conditions' => array('Person.group_id' => $group["Group"]['id']),
                'limit' => 100,
                'recursive' => 0
            )
        );

        $people = $this->paginate('Person');

        $this->set('people', $people);

    }
}        

这是视图:

<table>
    <tr>
        <th><?php echo $this->Paginator->sort('Person.id','Id'); ?></th>         
        <th><?php echo $this->Paginator->sort('first_name', 'First Name'); ?></th>         
        <th><?php echo $this->Paginator->sort('last_name', 'Last Name'); ?></th>         
    </tr>
    <!-- Here is where we loop through the people -->
    <?php foreach ($people as $person): ?>
    <tr>
        <td><?php echo $person['Person']['id']; ?></td>
        <td><?php echo $person['Person']['first_name']; ?></td>
        <td><?php echo $person['Person']['last_name']; ?></td>
    </tr>
    <?php endforeach; ?>
    <?php unset($player); ?>
</table>

【问题讨论】:

  • 我同意@RichardAtHome - 没有理由不使用 Paginator - 如果您不想要页面,只需将限制设置为无法访问的数字。

标签: cakephp cakephp-2.0 cakephp-2.1


【解决方案1】:

它是处理列排序的分页组件。如果您不想使用分页器(为什么不呢?),您必须编写自己的代码来执行此操作。

您可以对代码做的一个小调整是:

'limit' => $this->Group->find('count')

这将确保您始终有足够的限制来显示完整的表格。

【讨论】:

  • 我不想使用分页的原因是因为我想一次显示完整的团队名单。我不想有分页。我确实喜欢'limit' =&gt; $this-&gt;Group-&gt;find('count') 的建议,但是它增加了一个额外的数据库查询。
  • @bigmike7801 @bigmike7801 $this-&gt;Paginator-&gt;sort() 是为了再次获取记录,但按其使用的列排序,并按以下顺序首先单击:ASC,然后单击 DESC,nextClick ASC... 那里没有理由你不想只使用排序功能。此外,对一个从不超过 50 条记录的表进行额外的 COUNT 查询也不会是开销——你至少会有一个主索引——那么COUNT(*) 应该持续 0.0001 秒。 Here's 对此进行更多说明。
猜你喜欢
  • 1970-01-01
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
  • 2016-03-13
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
相关资源
最近更新 更多