Cakephp 2.0分页问题。如何在分页链接中传递参数?

时间:2012-02-03 06:15:13

标签: cakephp cakephp-2.0

Cakephp 2.0分页问题。如何在分页链接中传递参数?

我传递了像

这样的论点
 $this->Paginator->options(array('url' =>  $this->request->query));

但是在第二页之后它无法正常工作。

在第一页上提交的值将来自$this->request->query,但是来自第二页 它进入$this->params->named但需要进入$this->request->query

视图文件中的分页部分:

<div class="paging">
    <?php
    $this->Paginator->options(array('url' =>  $this->request->query));
        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'));
    ?>

在第一页上打印参数:

[query] => Array
    (
        [first_name] => 
        [username] => an
        [email] => 
        [dob] => Array
            (
                [year] => 
                [month] => 
                [day] => 
            )

    )

在第二页上打印参数:

    [named] => Array
            (
                [first_name] => 
                [username] => an
                [email] => 
                [dob%5Byear%5D] => 
                [dob%5Bmonth%5D] => 
                [dob%5Bday%5D] => 
                )
[query] => Array
    (
        [/admin/users/index/first_name:/username:an/email:/dob] => Array
            (
                [year] => 
            )

    )
</div>

2 个答案:

答案 0 :(得分:1)

According the the docsurl的{​​{1}}选项包含以下3个子选项:

  • 排序
  • 方向

您的PaginatorHelper::options没有定义这些键。我不确定为什么它适用于第一页而不是其他页面,但是你当前的结构是不正确的。

如果您担心将查询字符串值传递到下一页,请记住:

  

默认情况下,PaginatorHelper将合并所有当前传递和命名参数。所以你不必在每个视图文件中都这样做。

答案 1 :(得分:1)

我在View文件中添加了以下代码,现在每件事都运行正常。在所有页面中,参数值都在请求 - >查询

$url = $this->params['url'];

    unset($url['url']);
    if(isset($this->request->data) && !empty($this->request->data)) {
        foreach($this->request->data[$model_name] as $key=>$value)
            $url[$key] = $value;
    }
    $get_var = http_build_query($url);

    $arg1 = array();
    $arg2 = array();
    //take the named url
    if(!empty($this->params['named']))
        $arg1 = $this->params['named'];

    //take the pass arguments
    if(!empty($this->params['pass']))
        $arg2 = $this->params['pass'];

    //merge named and pass
    $args = array_merge($arg1,$arg2);

    //add get variables
    $args["?"] = $get_var;
    $this->Paginator->options(array('url' =>  $args)); 

在Controller动作方法中,我添加了以下内容

    $args = $this->params['url'];
    unset($args['url']);
    if(isset($args['submit'])) 
        unset($args['submit']);
    if(!empty($this->request->data)){
        foreach($this->data['User'] as $key=>$value)
            $args[$key] = $value;
    }else{
        foreach($args as $key=>$value)
            $this->request->data['User'][$key] = $value;
    }