简体   繁体   English

cakephp-如何更改分页条件?

[英]cakephp - how to change pagination condition?

on several places in same controller i have code like: 在同一控制器中的几个地方,我有如下代码:

$publications = $this->paginate('Publication', $conditions);

...where $conditions is array of conditions, which fields to select and how (using LIKE in mysql). ...其中$ conditions是条件数组,选择哪些字段以及选择方式(在mysql中使用LIKE)。

every one of them have different "Order by" statement. 他们每个人都有不同的“订购依据”声明。

how can i achieve that, and define different "order by" part of sql? 我怎样才能做到这一点,并定义不同的SQL“排序依据”部分?

UPDATE (2011-03-11): this is part how i defined pagination: 更新(2011-03-11):这是我如何定义分页的一部分:

$this->paginate = array(
        'limit' => 1, 
        'fields' => array('`Publication`.*, PublicationNumeration.*, Collection.*, Publisher.*, Publication.title as PublicationTitle'), 
        'joins' => array( array('table' => 'publication_numerations', 'alias' => 'PublicationNumeration', 'type' => 'LEFT', 'conditions' => array( 'Publication.id = PublicationNumeration.publication_id' ) ) ) 
        , 'order' => array('PublicationTitle desc')
      );

            $publications = $this->paginate('Publication', $conditions);
            $this->set(compact('publications', 'urlArgs'));

part with 'limit' works, but 'order' does not works, and does not show in debug mode at all. 具有“限制”的部分有效,但“顺序”无效,并且根本不在调试模式下显示。

You can override the pagination function for each time that you implement it in each different action. 您可以在每次执行每个不同操作时覆盖分页功能。 For example: 例如:

function list_recipes() {
    $this->paginate = array(
        'limit' => 10,
        'order' => array(
            'Recipe.title' => 'asc'
        )
    );
    $data = $this->paginate('Recipe');
    $this->set(compact('data'));
}

In your case, you'd probably want to reuse your $conditions array but alter the value of 'order' for each different implementation. 在您的情况下,您可能想重用$conditions数组,但为每个不同的实现更改'order'的值。

Further Reference: Pagination doc 进一步参考: 分页文档

Try this: 尝试这个:
echo $this->Paginator->link('5',array('limit' => '5')) echo $this->Paginator->link('10',array('limit' => '10')); echo $ this-> Paginator-> link('5',array('limit'=>'5'))echo $ this-> Paginator-> link('10',array('limit'=>'10' )); echo $this->Paginator->link('10',array('limit' => '25')); echo $ this-> Paginator-> link('10',array('limit'=>'25'));

I used this idea to create a custom selection of items per page. 我用这个想法创建了每页项目的自定义选择。
My default pagination: 我的默认分页:

var $paginate = array( 'order' => array('User.id' => 'asc') );

I added this to my controller to change the limit to 25 (also added 1 for 5 and 10 just to see how it would go and changed the limit value accordingly): 我将其添加到控制器中以将限制更改为25(还为5和10添加了1,只是为了查看其进行方式并相应地更改了限制值):

function list25() {
//fetches with different limit than default

    $this->paginate = array(
        'limit' => 25,
        'order' => array(
            'User.id' => 'asc'
        )
    );
    $data = $this->paginate('User');
    $this->set('users', $data); 
}

Then, in my main view (index.ctp) I added this at the bottom of the data listing: 然后,在主视图(index.ctp)中,将其添加到数据列表的底部:

<span style="float:right;">
<?php echo "&nbsp;&nbsp;<span style='vertical-align:top;font-size:12px;'>Show:</span>";
      echo $html->link(' 5', array('controller'=>'users', 'action' => 'list5'));
      echo $html->link(' 10', array('controller'=>'users', 'action' => 'list10'));
      echo $html->link(' 25', array('controller'=>'users', 'action' => 'list25'));
    ?>
</span>
<span style="float:right;margin-right:10px;"><?php  echo $paginator->numbers(); ?></span>   

...which shows the following text at the bottom of the display: Show: 5 10 25 ...在显示屏底部显示以下文本: 显示:5 10 25
The numbers, when clicked, are linked to the controller action and thus resets the limit (and refreshes the page with the new limit set). 单击数字时,这些数字将链接到控制器操作,从而重置限制(并使用新的限制设置刷新页面)。

Perhaps there is a better way to achieve the same functionality as I had to create a view for each option: list5.ctp, list10.ctp and list25.ctp. 也许有一种更好的方法来实现与我为每个选项创建一个视图相同的功能:list5.ctp,list10.ctp和list25.ctp。 Anyway, it provides the user a chance to choose how much data they want to see on a page. 无论如何,它为用户提供了选择在页面上看到多少数据的机会。 Comments? 评论?

....noob cakePHP user... .... noob cakePHP用户...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM