【问题标题】:CakePHP Pagination and FilterCakePHP 分页和过滤器
【发布时间】:2014-02-03 04:08:25
【问题描述】:

我为表格启用了分页。单击页码会创建一个附加了 /page:(page_number) 的 url。分页非常适合排序和过滤。

问题是:

  • 如果我在第 4 页
  • 然后执行搜索或过滤

结果:结果将从第 4 页开始显示。我想从第 1 页开始。

如果搜索结果只有一页,这一点尤其重要。然后我得到一个错误。

传递给 BeforeFind、AfterFind、BeforeFilter 和 AfterFilter 的 $query 似乎没有表明这是分页点击还是搜索或过滤器。

【问题讨论】:

    标签: sorting cakephp filter pagination


    【解决方案1】:

    您始终可以使用简单的$this->paginate['YourModel']['page'] = 1; 手动更改控制器中的当前页面,当然,在您调用$this->paginate() 之前。因此,在控制器中,您只需检查当前是否有请求的过滤器/搜索:

    if ($filter = $this->request->query('filter')) {
        // apply the filter, most likely as a WHERE condition for pagination, not sure what is your case...
    
        // and force to display the first page
        $this->paginate['YourModel']['page'] = 1;
    }
    

    但是,我在决定是首次应用过滤器/搜索还是仅将搜索结果浏览到下一页时发现问题。您可以将此信息存储到会话中,但感觉不太聪明。也许比较请求的页面和当前页面是否相同,但是在修改搜索词时它将不起作用。所以我认为最简单的方法是添加隐藏的表单字段:

    echo $this->Form->hidden('page', array('value' => 1));
    

    因此,当您搜索/过滤时,它会自动重置页面,而无需我之前编写的控制器代码。 如果你不使用经典的表单提交并通过 AJAX 提交,你可以附加查询参数page=1,结果是一样的。希望它可以帮助您前进

    【讨论】:

    • 在表单中隐藏页码的好主意!如果在我看到您的答案之前结果不存在,我最终会使用重定向到第 1 页。感谢您帮助我!
    • 是的,重定向是一种蛮力;)
    【解决方案2】:

    哇。我终于通过控制器中的重定向轻松获得了它。

    由于我不知道的原因,这不起作用。 $movieStars 已正确填充,但有些东西破坏了它:

    try {
            $movieStars = $this->paginate();
        } catch (NotFoundException $e) {
            // If page results are not found
            // For example - we are on page and we search for something 
            // Results returned for page 7 - but only two results
    
            // Not sure why this doesn't work - $movieStars are properly set and code continues
            $request = $this->request;
            $theseParams = $this->request->params;
            $pageFromParams = $theseParams['named']['page'];
            $passedArgs = $this->passedArgs;
            $pageFromArgs = $passedArgs['page'];
    
            $breakme = true;
    
            $this->request->params['named']['page'] = '1';
            $this->passedArgs['page'] = '1';
    
            $requestHere = $this->request->here;
            $requestUrl = $this->request->url;
    
            $newHere = preg_replace('/page:(\d+)/', 'page:1', $requestHere);
            $newUrl = preg_replace('/page:(\d+)/', 'page:1', $requestUrl);
    
            $this->request->here = $newHere;
            $this->request->url = $newUrl;
    
            $movieStars = $this->paginate();
        }
    
        $this->set(compact('movieStars'));
    

    但是这个小重定向技巧做到了。

    try {
            $movieStars = $this->paginate();
        } catch (NotFoundException $e) {
    
            // finally found easy solution if paginate throws error - redirect to page one
            $requestUrl = $this->request->url;
            $newUrl = preg_replace('/page:(\d+)/', 'page:1', $requestUrl);
            $redirectUrl = preg_replace('/movie_stars\//', '', $newUrl);
            $this->redirect($redirectUrl);
    
        }
    

    【讨论】:

      【解决方案3】:

      您可以手动设置表单操作 url 而不是蛋糕自动 url 。像这样: $this->Form->create('ModelName',array('url' => array('action' => 'index'));

      因为 cakephp 会自动生成表单 url,它还会在 url 中附加当前查询字符串。

      【讨论】:

        【解决方案4】:

        对我来说,我必须将表单的 url 放在视图中,然后它总是提交到一个干净的 url 并且页面参数消失

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-15
          • 1970-01-01
          • 2015-08-12
          • 2019-08-17
          • 2012-08-28
          • 1970-01-01
          相关资源
          最近更新 更多