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

问题描述 投票:0回答:2

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>
php cakephp cakephp-2.0
2个回答
1
投票

根据文档

url
PaginatorHelper::options
选项有以下3个子选项:

  • 排序
  • 方向

您的

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

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

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


1
投票

我在视图文件中添加了以下代码,现在一切正常。在所有页面中,参数值都来自请求->查询

$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)); 

在控制器操作方法中我添加了以下内容

    $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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.