doujiang1993 2013-04-18 12:08
浏览 66
已采纳

CArrayDataProvider与CGridView分页Yii

I'm trying to do a pagination on a CGridView using CArrayDataProvider (my $rawData is a custom array - not from a DB/model). So, In the controller`s action a have the following:

$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,
            ),
        ));

And:

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

On index.php I have:

...
<?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,
));

The first page is displayed correctly but when I select another page, my filter is lost and all data are displayed in the grid instead of "filtered" ones.

  • 写回答

2条回答 默认 最新

  • dongyi1996 2013-04-18 14:12
    关注

    Not sure it will solve your problem, but in your CArrayDataProvider you use id to define the name of the key field instead of keyField. You could try the following:

    $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条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗