2013-12-22 124 views
0

我有一个搜索表单,它返回一堆图像。由于他们的人数可能很高,我宁愿使用分页显示结果。但是,我似乎无法让它正常工作。我已将所有内容都移到了视图中,以尽可能简化事情。使用Yii搜索结果分页

这里是我的搜索和分页代码片段:

$searchData = $model->search(); // this is the result of the search 
$pages = new CPagination($searchData->itemCount); 
$searchData->setPagination($pages); 
$pages->applyLimit($searchData->criteria); 
$pages->pageSize = 2; 

然后,我显示使用 “的foreach” 语句的结果:

foreach($searchData->getData() as $data) 
{ 
// results displayed here 
} 

最后,我有CLinkPager部件:

$this->widget('CLinkPager', array(
    'pages' => $pages, 
    'header' => 'Отиди на страница: ', 
    'nextPageLabel' => 'Следваща', 
    'prevPageLabel' => 'Предишна', 
        )); ` 

问题是:控件和显示之间没有关系结果。例如,如果我得到4个结果,小部件正确显示有2个页面。但是,结果不会显示在两个页面中,全部显示在一个页面中,单击CLinkPager链接将不会执行任何操作。

我以前成功地完成了分页,但是对于所有使用 model() - > findAll()的数据。

如何说明分页控件与搜索结果之间的关系...?

谢谢你的时间。

+1

[在这个网站采取lokk](http://www.yiiframework.com/forum/index.php/topic/25901-pagination-solution-simple-and-complex-ways/) –

+0

这不完全是我在找什么,但是我从链接中获得了一些灵感,并设法解决了我的问题。真诚的谢谢! 诀窍是从$ model-> search()获取查询条件,然后在该条件下使用$ model-> findAll()。 – NovaLogic

回答

2

我认为你可能过于复杂化。您可以更轻松地使用CListView小部件通过分页显示结果。

你的控制器可能有这个;

public function actionIndex(){ 
    $model = new Model; 
    $this->render('index', array('model' => $model); 
} 

你的模型需要有一个返回CActiveDataProvider实例的搜索方法,例如

public function search() { 
     $criteria = new CDbCriteria; 
     $criteria->compare('id', $this->id, true); 
     $criteria->compare('title', $this->title, true); 
     $criteria->order = 'sortOrder'; 
     return new CActiveDataProvider($this, array(
      'criteria' => $criteria, 
     )); 
    } 

您认为这些文件,你可以使用此代码;

$this->widget('widgets.CListView', array(
    'dataProvider' => $model->search(), 
    'itemView' => '_list', 
    'template' => '{pager}{items}', 
    'pager' => array(
     'nextPageLabel' => 'Следваща', 
     'prevPageLabel' => 'Предишна', 
    ) 
)); 
0

你可以把这个在您的扩展文件夹扩展CListView中,并改变页面大小那里。

<?php 
Yii::import('zii.widgets.CListView'); 

class CustomListView extends CListView { 
    public function init() { 
     if ($this->dataProvider !== null) 
      $this->dataProvider->pagination->pageSize = 30; 

     $this->dataProvider->pagination->nextPageLabel => 'Следваща'; 
     $this->dataProvider->pagination->prevPageLabel => 'Предишна'; 
     parent::init(); 
    } 
} 

然后,使用CustomListView如下:

<?php $this->widget('CustomListView', array(
    'dataProvider'=>$dataProvider, 
    'itemView'=>'_view', 
)); ?> 

你并不需要使用CLinkPager部件直接这样。