CArrayDataProvider 与 CGridView 分页 Yii [英] CArrayDataProvider with CGridView pagination Yii

查看:16
本文介绍了CArrayDataProvider 与 CGridView 分页 Yii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

还有:

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

在 index.php 上我有:

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.

推荐答案

不确定它会解决您的问题,但在您的 CArrayDataProvider 中您使用 id 来定义键字段的名称而不是 <代码>keyField.您可以尝试以下操作:

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

这篇关于CArrayDataProvider 与 CGridView 分页 Yii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆