【问题标题】:Pagination in requestActionrequestAction 中的分页
【发布时间】:2013-09-13 04:56:54
【问题描述】:

我正在构建一个动态视图 (Page),它由多个通过 $this->element('messages_unread') 调用的元素(小部件)组成。其中一些元素需要与页面模型相关的数据。
在现实生活中的话:我的用户将能够通过从众多元素中进行选择来构建自己的页面(“前 5 个帖子”、“10 条未读消息”等)

我通过在元素内调用$this->requestAction(array('controller'=>'events','action'=>'archive') 来获取数据,每个元素的 url 变量都不同。

我知道requestAction() 很昂贵,我计划通过适当的缓存来限制成本。

实际问题:
我的问题是分页。当我在Page 视图中并调用requestAction('/events/archive') 时,页面视图中的PaginatorHelper 将不知道Event 模型及其分页器变量和$this->Paginator->next() 等......将不起作用。
如何实现正确的分页?我尝试通过调用$this->Paginator->options(array('model'=>'Event')) 来设置模型,但这不起作用。
我是否可能需要在requestAction 中返回自定义定义的分页变量,从而构建我自己的?

或者是否有另一种方法甚至可以避免requestAction()?请记住,请求的数据与页面无关。

亲切的问候, 巴特

[编辑] 我的临时解决方案,但仍对 cme​​ts/解决方案开放:
在 requestedAction Event/archive 中,返回分页器变量以及如下数据: return array('data'=>$this->paginate(), 'paging' => $this->params['paging']);

【问题讨论】:

    标签: cakephp pagination cakephp-2.4


    【解决方案1】:

    我做了更多修改,以下对我有用,并且 PaginationHelper 有效:

    在元素中:

    // requestAction returns an array('data'=>... , 'paging'=>...)
    $data = $this->requestAction(array('controller'=>'events','action'=>'archive'));  
    
    // if the 'paging' variable is populated, merge it with the already present paging variable in $this->params. This will make sure the PaginatorHelper works
    if(!isset($this->params['paging'])) $this->params['paging'] = array();
    $this->params['paging'] = array_merge( $this->params['paging'] , $data['paging'] );
    
    foreach($data['events'] as $event) {
        // loop through data...
    }
    

    在控制器中:

    public function archive() {
        $this->paginate = array(
            'limit'     => 10
        );
    
        if ($this->params['requested'])
            return array('events'=>$this->paginate('Event'), 'paging' => $this->params['paging']);
    
        $this->set('events', $this->paginate('Event') );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多