【问题标题】:How to implement pagination with search in CakePHP如何在 CakePHP 中使用搜索实现分页
【发布时间】:2015-08-13 02:43:40
【问题描述】:

我的分页工作正常,甚至搜索也工作正常。但是我遇到的问题是当我单击分页链接中的下一页链接时。搜索不适用于分页的下一页。我还需要知道如何通过 url 发送其他参数,并在分页查询中使用它们。我需要这方面的帮助,因为我是 CakePHP 的新手。

在控制器页面中我使用了以下代码:

class StatesController extends AppController {

    public $components = array('Paginator');

    public $paginate = array(
        'limit' => 2,
        'fields' => array('State.id', 'State.state','State.code'),
        'order' => array(
            'State.state' => 'asc'
        )
    );

    public function admin_index() {
        $this->layout = false;
        $this->layout = 'adminlayout';

        //****** pagination starts 

        $search=$this->request->data('State.search');

        $this->Paginator->settings = $this->paginate;
        // similar to findAll(), but fetches paged results
        $stateListAr = $this->Paginator->paginate('State',
            array('State.state LIKE' => "%".$search."%")
        );
        $this->set('stateListAr', $stateListAr);

        //****** pagination ends 

        $this->set('stateListAr',$stateListAr);
        $this->render('admin_index');   
    }
}

在视图页面中我使用了以下代码:

<?php echo $this->Paginator->prev('« Previous', null, null, array('class' => 'disabled')); ?>
<?php echo $this->Paginator->numbers(array('first' => 'First page')); ?>                  
<?php echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled')); ?> 

【问题讨论】:

  • 请解释当您说搜索不适用于下一页分页时的意思。你的意思是原来的搜索过滤器丢失了吗?
  • 您能否提供一个实时网址来查看您目前拥有的内容?如果那不可能,至少请发布您的整个视图的源代码。另请指定您尝试通过 url 传递的额外参数。
  • 是的@ChicagoSky,原始搜索不适用于下一页的分页。实际上应该传递该数据或 url 应该包含该搜索数据。我该怎么做。

标签: cakephp pagination cakephp-2.0


【解决方案1】:

我已经解决了我的问题,就是这样。我在视图页面中使用了代码,这里 $search 变量我已经从控制器设置了数据。 $搜索); $this->分页器->选项(数组( 'url' => $urlParamAr )); echo $this->Paginator->prev('«Previous', null, null, array('class' => 'disabled'));

 echo $this->Paginator->numbers(array('first' => 'First page'));

echo $this->Paginator->next('Next »', null, null, array('class' => 'disabled'));

?>

【讨论】:

    猜你喜欢
    • 2012-04-27
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    相关资源
    最近更新 更多