如何在Cakephp中进行多分页而不出现页面未找到错误

问题描述 投票:0回答:2

我在学生模型视图页面中对 2 个模型(收入、支出)进行了分页。 我确实遇到了 cakephp 分页的页面未发现问题。

分页结果只有1页或相同时没有任何问题,但如果分页结果多于另一页,就会出错。

for example.
-income has paginate result 1 page and expanse has 1 page. No problem at all.
-income has 10 pages and expanse has 10 pages no problem at all.
-income has 2 pages and expanse has 1 page. income page 2 page not found error.
-income has 5 pages and expanse has 2 pages. income page 3,4,5 page not found error.
-income has 10 pages and expanse has 13 pages. expanse page 11,12,13 page not found error.

例如(不是真实的),学生视图有收入和支出项目,两者都显示为分页。

//this is how I config paginator in Student controller.
public $paginate = array(
    'Income' => array (
            'order'=>array('income_id'=>'ASC'),
            'limit'=>10,
            'recursive'=>0  
    ),
    'Expense' => array (
            'order'=>array('expense_id'=>'ASC'),
            'limit'=>10,
            'recursive'=>0,

    )
);


<div class="paging">//this is how I config Income paginator in Student view
    <?php
        echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Income'), null, array('class' => 'prev disabled'));
        echo $this->Paginator->numbers(array('model'=>'Income','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
        echo $this->Paginator->next(__('next') . ' >', array('model'=>'Income'), null, array('class' => 'next disabled'));
    ?>
    </div>


//this is how I config Expanse paginator in Student view
    <div class="paging">
    <?php
        echo $this->Paginator->prev('< ' . __('previous'), array('model'=>'Expanse'), null, array('class' => 'prev disabled'));
        echo $this->Paginator->numbers(array('model'=>'Expanse','separator' => '','modulus'=>100,'first'=>'หน้าแรก','last'=>'หน้าสุดท้าย'));
        echo $this->Paginator->next(__('next') . ' >', array('model'=>'Expanse'), null, array('class' => 'next disabled'));
    ?>
    </div>

请帮助我。对不起我的英语不好 如果您有任何疑问,请问我。 谢谢你。

php cakephp pagination paginator
2个回答
1
投票

是的,这是可能的......

Step1 : 在View/Elements/paging.ctp中制作paging.ctp

<?php if (!isset($this->Paginator->params['paging'])) {
  return;
}
if (!isset($model) || $this->Paginator->params['paging'][$model]['pageCount'] < 2) {
  return;
}
if (!isset($options)) {
  $options = array();
}
$options['model'] = $model;
$options['url']['model'] = $model;
$this->Paginator->__defaultModel = $model;

?>
<div class="paging">
  <ul class="pagination pull-right">
    <?php
        echo $this->Paginator->prev('<<', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
        echo $this->Paginator->numbers(am($options,array('tag' => 'li', 'separator' => '', 'currentClass' => 'active', 'currentTag' => 'a')));
        echo $this->Paginator->next('>>', array_merge(array('class' => '', 'tag' => 'li'), $options), null, array('class' => 'disabled', 'tag' => 'li'));
    ?>
  </ul>

</div>

Step2:现在将此功能添加到Controllers/AppController

public function pageForPagination($model) {
        $page = 1;

        $sameModel = isset($this->request->params['named']['model']) && $this->request->params['named']['model'] == $model;
        $pageInUrl = isset($this->request->params['named']['page']);
        if ($sameModel && $pageInUrl) {
            $page = $this->request->params['named']['page'];
        }

        $this->passedArgs['page'] = $page;
        return $page;
    }

第三步:现在在控制器操作中执行一些条件,以便调用正确的页面

if (empty($this->request->params['named'])) {
$page = $this->pageForPagination('Income');
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
    'Income' => array (
            'order'=>array('income_id'=>'ASC'),
            'limit'=>10,
            'page' => $page,
            'recursive'=>0  
    ),
    'Expense' => array (
            'order'=>array('expense_id'=>'ASC'),
            'limit'=>10,
            'page' => $page1,
            'recursive'=>0,

    ));
} else if ($this->request->params['named']['model'] == 'Income') {
$page = $this->pageForPagination('Income');
public $paginate = array(
    'Income' => array (
            'order'=>array('income_id'=>'ASC'),
            'limit'=>10,
            'page' => $page,
            'recursive'=>0  

));
} else if ($this->request->params['named']['model'] == 'Expense') {
$page1 = $this->pageForPagination('Expense');
public $paginate = array(
    'Expense' => array (
            'order'=>array('income_id'=>'ASC'),
            'limit'=>10,
            'page' => $page1,
            'recursive'=>0  

  ));

第 4 步:现在在学生视图中调用分页元素

<?php echo $this->element('paging', array('model' => 'Income'));?>
 <?php echo $this->element('paging', array('model' => 'Expense'));?>

注意:请注意括号和分号....抱歉迟到了,但会帮助其他人...谢谢..


0
投票

我认为如果你使用两个不同的 iframe 加载不同的 URL 进行分页,你可以处理这个问题。 或者你必须调用 ajax 函数在一页中对 2 个模型进行分页,而无需重新加载页面....

© www.soinside.com 2019 - 2024. All rights reserved.