CArrayDataProvider与CGridView分页Yii

时间:2013-04-18 12:08:46

标签: php yii cgridview

我正在尝试使用CGridViewCArrayDataProvider进行分页(我的$rawData是一个自定义数组 - 而不是来自数据库/模型)。 因此,在控制器的操作中,a具有以下内容:

$form = new SearchUser;//here I have SearchUser form that extends CFormModel with the following attributes: 'id', 'name', 'surname', 'phone', 'address'
$users = array();
if (isset($_POST['SearchUser'])) {
....//prepare users array from my custom source-> not from DB/models etc
}

$dataProvider=new CArrayDataProvider($users, array(
            'id'=>'id',
            'keys'=>array('name', 'surname', 'phone', 'address'),
            'sort'=>array(
                'attributes'=>array(
                    'name', 'surname', 'phone', 'address'
                ),
            ),
            'pagination'=>array(
                'pageSize'=>15,
            ),
        ));

并且:

$this->render('index', array('dataProvider'=>$dataProvider, 'form'=>$form));

在index.php上我有:

...
<?php echo CHtml::link('Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$form,
)); ?>
</div><!-- search-form -->
<?php

$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(

    array(
        'name' => 'Name',          
        'type' => 'raw',
        'value' => 'CHtml::encode(@$data["name"])'
    ),
    array(
        'name' => 'Surname',          
        'type' => 'raw',
        'value' => 'CHtml::encode(@$data["surname"])'
    ),/*
    array(
        'name' => 'Phone',          
        'type' => 'raw',
        'value' => 'CHtml::encode(@$data["phone"])'
    ),*/
    array(
        'name' => 'Address',          
        'type' => 'raw',
        'value' => 'CHtml::encode(@$data["address"])'
    ),
),
'enablePagination'=> true,
));

第一页显示正确但当我选择另一页时,我的过滤器丢失,所有数据都显示在网格中而不是“已过滤”。

2 个答案:

答案 0 :(得分:1)

不确定它是否会解决您的问题,但在您的CArrayDataProvider中,您使用id来定义关键字段的名称,而不是keyField。 您可以尝试以下方法:

$dataProvider=new CArrayDataProvider($users, array(
    'id'=>'users',
    'keyField' => 'id', 
    'keys'=>array('id','name', 'surname', 'phone', 'address'),
    'sort'=>array(
        'attributes'=>array(
            'name', 'surname', 'phone', 'address'
        ),
    ),
    'pagination'=>array(
        'pageSize'=>15,
    ),
));

答案 1 :(得分:0)

  

用户型号:

    public function getAllList(){
        $sql = "SELECT * FROM users";
        $cmd = Yii::app()->db->createCommand($sql);
        return $cmd->queryAll();
    }
  

用户控制器:

    public function actionIndex(){
        $model = Users::model()->getAllList();
        $dataProvider = new ArrayDataProvider($model, array(
            'keyField'          =>  'user_id',
            'pagination'        =>  array(
                'pageSize'  =>  5,
                ),
            )
        );
        $this->render('index',array(
            'dataProvider'  =>  $dataProvider,
            )
        );
    }
  

用户观点:

index.php

    $this->widget('zii.widgets.CListView', array(
        'ajaxUpdate'        =>  true,
        'dataProvider'      =>  $dataProvider,
        'itemView'          =>  '_view',   // refers to the partial view named '_carList'
        'template'          =>  '<h4><span>{summary}</span></h4>{items}{summary}{pager}',
        'pagerCssClass'     =>  'pagination',
        'summaryText'       =>  '{start}-{end} of {count} Total Property ',
        'pager'             =>  array(
            'header'                =>  FALSE,
            'firstPageLabel'        =>  FALSE,
            'lastPageLabel'         =>  FALSE,
            'nextPageLabel'         =>  'Next',
            'prevPageLabel'         =>  'Previous',
            'selectedPageCssClass'  =>  'active',
            'maxButtonCount'        =>  '5'
        ),
        'beforeAjaxUpdate'      =>  'function(){
            //todo before
        }',
        'afterAjaxUpdate'       =>  'function(){
            //todo after
        }',
    ));

_view.php

    <p>
        <?php echo $data['user_id']; ?>
        <?php echo $data['user_name']; ?>
        <?php echo $data['user_pass']; ?>
    </p>