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 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答