YII CArrayDataProvider 的使用

利用CArrayDataProvider,可以对一个数组里面的数据进行分页
action代码:

public function actionPage(){
            $rawData = array(
                array('id'=>1,'username'=>'aa','password'=>'aaaaaa'),
                array('id'=>2,'username'=>'bb','password'=>'aaaaaa'),
                array('id'=>3,'username'=>'cc','password'=>'aaaaaa'),
                array('id'=>4,'username'=>'dd','password'=>'aaaaaa'),
                array('id'=>5,'username'=>'ee','password'=>'aaaaaa'),
                array('id'=>6,'username'=>'ff','password'=>'aaaaaa'),
                array('id'=>7,'username'=>'gg','password'=>'aaaaaa'),
                array('id'=>8,'username'=>'hh','password'=>'aaaaaa'),
                array('id'=>9,'username'=>'jj','password'=>'aaaaaa'),
                array('id'=>10,'username'=>'qq','password'=>'aaaaaa'),
                array('id'=>11,'username'=>'www','password'=>'aaaaaa'),
                array('id'=>12,'username'=>'xx','password'=>'aaaaaa'),
                array('id'=>13,'username'=>'zz','password'=>'aaaaaa'),
            );
 
            $dataProvider=new CArrayDataProvider($rawData, array(
                'id'=>'user',
                'sort'=>array(
                    'attributes'=>array(
                         'id', 'username', 'password',
                    ),
                ),
                'pagination'=>array(
                    'pageSize'=>10,
                ),
            ));
            $this->render('page',array('dataProvider'=>$dataProvider));
        }

views代码:

 $this->widget('zii.widgets.grid.CGridView', array(
            'id'=>'effectivepolicy-grid',
            'dataProvider'=>$dataProvider,
            'emptyText'=>'没有找到数据.',
            'nullDisplay'=>'-',
            'columns'=>array(
 
                array(
                    'name'=>'用户名',
                    'value'=>'$data["username"]',
                    'htmlOptions'=>array('style'=>'width:55px;')
                ),
                                array(
                    'name'=>'密码',
                    'value'=>'$data["password"]',
                    'htmlOptions'=>array('style'=>'width:55px;')
                ),
            ),
        ));

发表评论

电子邮件地址不会被公开。 必填项已用*标注