简体   繁体   English

奇怪的CakePHP(1.3)分页组件行为

[英]Weird CakePHP (1.3) Pagination component behaviour


I have a users list page where the result can be modified by using gender, city and country selects (dropdowns), and it's working perfectly fine. 我有一个用户列表页面,可以在其中使用性别,城市和国家/地区选择(下拉列表)来修改结果,并且效果很好。 As we have a lot of users I need to put a pagination as well, and here comes the "weird" part (I am sure it's not weird but I just cannot figure out where the problem comes from): when I select a gender, the pagination works great and I can navigate between all of the pages, but if I select a city for instance (in addition or not of the gender), the pagination numbers are right but I lost the city restriction when I move to another page. 由于我们有很多用户,因此我也需要进行分页,这是“怪异”的部分(我敢肯定这并不奇怪,但我只是无法弄清问题的出处):当我选择性别时,分页效果很好,我可以在所有页面之间导航,但是,例如,如果我选择一个城市(添加或不添加性别),则分页号码是正确的,但是当我移至另一页时,我失去了城市限制。


So I tried to understand what happens to my filters by displaying the $this->data . 所以我试图通过显示$this->data来了解过滤器发生了什么。 And it says exactly the same as before: working perfectly fine with the gender ( $this->data['users']['gender'] go through all the pagination pages), but the other parameters just got lost once I try to navigate away. 它说的和以前完全一样:在性别上正常工作( $this->data['users']['gender']遍历所有分页页面),但是一旦我尝试了其他参数,这些参数就丢失了导航。
The thing is that there isn't any difference between the filter gender and the other ones, either on the controller side or in the view (both are select inputs). 事实是,过滤器gender与其他过滤器gender之间没有任何区别,无论是在控制器端还是在视图中(均为选择输入)。

On a more technical side, here's a bit of my code: 从技术角度来看,这是我的一些代码:

    //In the controller function
    if (!empty($this->data['users']['gender'])) {
        $conditions['gender'] = $this->data['users']['gender'];
    }
    if (!empty($this->data['users']['country_id'])) {
        $conditions['city_id'] = 
            $this->User->City->find(
                'list', 
                array(
                    'conditions' => array(
                        'country_id' => $this->data['users']['country_id']), 
                    'fields' => 'City.id'));
    }
    if (!empty($this->data['users']['city_id'])) {
        if($this->data['users']['city_id'] == 'NULL') {
            $conditions['city_id IS ?'] = NULL;
        } else {
            $conditions['city_id'] = $this->data['users']['city_id'];
        }
    }
    //debug($this->data);

    $options = array(
        'limit' => 20,
        'order' => 'User.lastname ASC',
        'conditions' => $conditions);
    $this->paginate = $options;
    $users = $this->paginate('User');


As you can see, I use the paginate() function within the controller. 如您所见,我在控制器中使用了paginate()函数。 I still don't understand why it's working for the gender filter and not the rest 我仍然不明白为什么它适用于性别过滤器,而不是其余部分


Cheers, 干杯,
Nicolas. 尼古拉斯

Your problem is not in the controller, while in the helper. 您的问题不在控制器中,而在助手中。 When you pass the variable for the first time, it's working, because there is posted variable, but the pagination doesn't handle these variables unless you pass them to the pagination helper. 第一次传递变量时,它起作用了,因为其中有发布的变量,但是分页无法处理这些变量,除非您将它们传递给分页助手。

Read this article and in my opinion it's best to pass the gender, and city via _GET. 阅读本文 ,我认为最好通过_GET传递性别和城市。

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

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