【问题标题】:CakePHP How to Persist Search Querystring to Subsequent Paginator PagesCakePHP 如何将搜索查询字符串持久化到后续分页器页面
【发布时间】:2015-10-02 16:02:34
【问题描述】:

我有一个正确过滤的搜索页面,但是当它分页时,您会丢失数据,因为我正在发布它。所以我将它切换为 GET,但我不知道如何添加我传递回分页器的 http_build_query( $params )。

我尝试在选项的分页器 url 中设置查询参数,但没有运气,API 没有提到添加查询参数。

如何设置搜索结果查询参数,以便不同的页面知道它们被过滤的内容?因此,对 name=steve 和 company=SomeCompany 的搜索会一直持续到第 2、3、4 页,每页有 10 个结果,并且不会重置为显示所有未过滤的 100 个结果。

控制器中的简单分页示例

$this->paginate = [
    'limit'      => 5,
    'order'      => [ 'CollectionAgencyAgent.id' => 'desc' ]
];

return $this->paginate( $this->CollectionAgencyAgent, $conditions );

分页查看

<ul class="pagination <?php echo $class ?>">

    <?php
    $this->Paginator->options( [
        'url' => [
            'controller' => ''
        ]
    ] );
    ?>

    <?php echo $this->Paginator->prev( __( '&laquo; Previous' ), [
        'escape'      => false,
        'tag'         => 'li',
        'class'       => 'arrow',
        'disabledTag' => 'a'
    ] ); ?>

    <?php echo $this->Paginator->numbers( [
        'separator'  => '',
        'tag'        => 'li',
        'currentTag' => 'a'
    ] ); ?>

    <?php echo $this->Paginator->next( __( 'Next &raquo;' ), [
        'escape'      => false,
        'tag'         => 'li',
        'class'       => 'arrow',
        'disabledTag' => 'a'
    ] ); ?>
</ul>

【问题讨论】:

    标签: php cakephp pagination cakephp-2.6


    【解决方案1】:

    分页器类automatically merges the current request parameters:

    public function beforeRender($viewFile) {
        $this->options['url'] = array_merge($this->request->params['pass'], $this->request->params['named']);
        if (!empty($this->request->query)) {
            $this->options['url']['?'] = $this->request->query;
        }
        parent::beforeRender($viewFile);
    }
    

    所以简短的回答是什么都不做,这样就行了。

    它不适用于问题中的代码的原因是这个调用:

    $this->Paginator->options( [
        'url' => [
            'controller' => ''
        ]
    ] );
    

    这将清除分页器的运行时 url 选项。因此,为了防止问题中的问题 - 只需删除该呼叫(然后,可能修复提示您添加它的路由问题 =))。

    【讨论】:

    • 谢谢,是的,我们的路线看起来...至少可以说很时髦,我必须弄清楚他们在那里做了什么,所以我们不再收到找不到页面,因为我已经删除了你提到的选项.
    • 与我的 Route 问题相关,这似乎是由于在主路由 Router::connect( '/:action/*', [ 'controller' =&gt; 'sales', 'pass' =&gt; $subdomain ] ); 的 URL 中使用不想要控制器,其中子域是 `$subdomain = substr( env( "HTTP_HOST" ), 0, strpos( env( "HTTP_HOST" ), "." ) );,因为我们在销售域上sales.ourdomain.com 所以不想使用sales.ourdomain.com/sales/action...,但分页器想使用/sales/action/page:2。有没有更简单的方法来做到这一点?
    • 如果您对此感兴趣,我最终提出了一个问题stackoverflow.com/q/32918085/1148107
    猜你喜欢
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2016-12-20
    • 2023-03-05
    • 2021-01-28
    • 2014-07-05
    相关资源
    最近更新 更多