我有一个搜索页面,过滤正确,但当它分页你会失去数据,因为我是张贴它。所以我把它切换到GET,但我不知道如何添加http_build_query($params),我传递回paginator。
我试过在选项中设置paginator 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 );
VIEW with Pagination
<ul class="pagination <?php echo $class ?>">
<?php
$this->Paginator->options( [
'url' => [
'controller' => ''
]
] );
?>
<?php echo $this->Paginator->prev( __( '« 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 »' ), [
'escape' => false,
'tag' => 'li',
'class' => 'arrow',
'disabledTag' => 'a'
] ); ?>
</ul>
分页器类自动合并当前请求参数:
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' => ''
]
] );
这将清除Paginator的运行时url选项。因此,为了防止问题中的问题-只需删除该调用(然后,可能会修复提示您添加它的路由问题=))。