【问题标题】:cakePHP paginator not passing passedargscakePHP分页器没有传递passargs
【发布时间】:2012-05-02 11:56:39
【问题描述】:

我正在使用 cakePHP,当您单击不同的页面时,我正在尝试让分页器组件传递获取变量或传递的参数。我有各种不同的搜索输入选择器,它们“过滤”返回的结果。这适用于第一次查看,但当我点击不同的页面时,它会显示所有结果。

我的分页器有以下设置:

// In my controller class:
public $paginate = array('maxLimit' => 10, 'paramType' => 'querystring');

// Within my action method:
$this->paginate = array('conditions' => array(...),
                        order => array('Model.field ASC'),
                        'limit' => 20 
 );

 // Calling the paginator:
 $results = $this->paginate('Model');
 $this->set(compact('results'));

在我的视图文件中:

 <div class="paging">
<?php
    echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
    echo $this->Paginator->numbers(array('separator' => ''));
    echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</div>

编辑: 据我了解,最好使用passedArgs,但我有点不确定如何做到这一点。我的 $this->passedArgs 没有返回任何结果,所以我在我的控制器示例中创建了传递的参数。我还将表单从 Get 更改为 Post:

 $this->passedArgs["searchfield"] = $_POST["value"];

它现在在分页条中正确传递了passedArgs,但我不确定现在如何构建分页条件数组。在大多数情况下,用户不会选择默认值示例,其中一个过滤器是 date from 和 date to,然后是一个搜索输入框,如果我留下日期,它仍然会创建参数并且不返回任何结果,所以本质上是我的 url会是这样的:

 http://localhost/site/controller/action/page:3/datefrom:0/dateto:0/searchFor:survey

有什么帮助吗?

【问题讨论】:

    标签: cakephp components paginator


    【解决方案1】:

    您可以通过以下方式传递视图中的所有参数:

    $this->Paginator->options(array('url' => $this->passedArgs));
    

    或手动分配参数:

    $this->Paginator->options(array('url' => array("0", "1")));
    

    在回显分页器之前

    更多示例请参见CakePHP Cookbook

    【讨论】:

    • 嗨@Nebel54,谢谢你的建议。我想我发现了问题,那就是我的过滤器表单或搜索表单只是将值发布到 GET 所以我的 $this->passedArgs 是空的。您能否指出我是如何构建 passArgs 的?
    • 你能发布你的获取参数的样子吗?它们是像 /param1:true/param2:name 还是像 /?param1=true&param2=name
    • 它是 /param1:value/param2:value,但现在因为并非所有参数都会始终有值,所以它会执行 param1:0/param2:possiblevalue/ 这将破坏查询
    • 我不确定这是否是最干净的解决方案,但我会取消设置条件数组中不需要的默认值,例如if ($my_passed_args['datefrom']==0) { unset($my_passed_args['datefrom']);在您的操作方法中分配条件之前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多