0

我在学生模型视图页面中对 2 个模型(收入、扩展)进行了分页。我真的没有找到 cakephp 分页的问题。

当分页结果只有一页或相同的时候没有任何问题,但是如果一个分页结果比另一个多,就会出错。

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>

请帮我。对不起我的英语如果你有任何问题,请问我。谢谢你。

4

2 回答 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;
    }

Step3:现在在控制器动作中做一些条件,以便应该调用正确的页面

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'));?>

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

于 2014-08-28T07:34:55.417 回答
0

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

于 2014-03-10T13:49:43.340 回答