Cakephp and paginating multiple result sets

前端 未结 2 1700
后悔当初
后悔当初 2021-01-15 00:30

I have pagination working fine for when $this->paginate is called once in an action, but it does not appear to be very good support for multiple calls. Here is what I\'m loo

2条回答
  •  深忆病人
    2021-01-15 01:12

    Since there was not much activity on this, I went ahead and did the reasonable thing and figured out a workaround. Below is what I came up with:

    Controller:

    function index() {
        $active = $this->paginate('Post', array('Post.status'=>1));     
        $activePaging = $this->request->params['paging']['Post'];
    
        $inactive = $this->paginate('Post', array('Post.status'=>0));
        $inActivePaging = $this->request->params['paging']['Post'];
    
        $this->set('active', $active);
        $this->set('inactive', $inactive);
    
        $this->request->params['paging']['Active'] = $activePaging;
        $this->request->params['paging']['InActive'] = $inActivePaging;
        unset($this->request->params['paging']['Post']);
    }
    
    public function paginate() {
        if ($this->request->is('ajax')) {
            $model = $this->request->params['named']['model'];
            $status = $model === 'Active' ? 1 : 0;
    
            $posts = $this->paginate('Post', array('Post.status'=>$status));
            $paging = $this->request->params['paging']['Post'];
            unset($this->request->params['paging']['Post']);
            $this->request->params['paging'][$model] = $paging;
    
            $this->set('posts', $posts);
            $this->set('model', $model);
            $this->render('/Posts/ajax_posts_paginated', 'ajax');
        }
    }
    

    Views: (simplified)

    index.ctp

    Inactive Posts

    element('posts_table_paginated', array('posts'=>$inactive, 'model'=>'InActive')); ?>

    Active Posts

    element('posts_table_paginated', array('posts'=>$active, 'model'=>'Active')); ?>

    elements/posts_table_paginated.ctp

    Paginator->options(array(
        'update' => '#'.$model,
        'evalScripts' => true,
        'url'=> array('controller'=>'posts', 'action'=>'paginate', 'model'=>$model)
    ));
    ?>
    
    Paginator->sort('id', 'ID', array('model'=>$model)); ?> Paginator->sort('title', 'Title', array('model'=>$model)); ?> Paginator->sort('created', 'Created', array('model'=>$model)); ?> Created by
    params['paging'][$model]['count'] > 0) { ?> Js->writeBuffer(); ?>

提交回复
热议问题