Using CArrayDataProvider to populate a CGridView in Yii

Using CArrayDataProvider to populate a CGridView in Yii

Last updated:

This is how you'd go about using a regular(number-indexed) array (wrapped by a CArrayDataProvider) as Data Provider for a CGridView in Yii:

  • In the controller:

    <?php              
    $dp=new CArrayDataProvider(array('foo','bar','baz'),array(
             'keyField'=>false,
             'pagination'=>array(
                 'pageSize'=>30,
             ),
    ));
    
    $this->render('viewFile', array(
            'dataProvider'=>$dp,
    ));
    
  • in the view file:

    <?php 
    $this->widget('zii.widgets.grid.CGridView', array(
            'dataProvider'=>$dataProvider,
            'template'=>"{items}",
            'htmlOptions'=>array(
                'style'=>'margin:8px;'
            ),
            'columns'=>array(
                array(
                    'header'=>'Index',
                    'value'=>'$row',
                    'htmlOptions'=>array(
                        'style'=>'width:50px;'
                    )
                ),
                array(
                   'header'=>'Content',
                   'value'=>'$data'
                ),
            ),
    ));