CArrayDataProvider with CGridView pagination Yii

前端 未结 2 1407
花落未央
花落未央 2021-01-21 22:54

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 c

相关标签:
2条回答
  • Users Model:

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

    Users Controller:

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

    Users Views:

    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>
    
    0 讨论(0)
  • 2021-01-21 23:35

    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,
        ),
    ));
    
    0 讨论(0)
提交回复
热议问题