May 13 2013

Yii CGridView listdata with CArrayDataProvider

Tagged Under :

Yii
Yii, use CArrayDataProvider to listing out the array with CGridView widgets.

Below example are showing how to use it.
Data array:
$data = array();
$data[] = array('id' => 1, 'name' => 'name 1', 'age' => 22, 'date' => '1-Jan-2013');
$data[] = array('id' => 2, 'name' => 'name 2', 'age' => 25, 'date' => '1-Feb-2013');
$data[] = array('id' => 3, 'name' => 'name 3', 'age' => 22, 'date' => '1-Mac-2013');
$data[] = array('id' => 4, 'name' => 'name 4', 'age' => 28, 'date' => '1-Jan-2013');
$data[] = array('id' => 5, 'name' => 'name 5', 'age' => 23, 'date' => '1-Apr-2013');
$data[] = array('id' => 6, 'name' => 'name 6', 'age' => 29, 'date' => '1-May-2013');
$data[] = array('id' => 7, 'name' => 'name 7', 'age' => 21, 'date' => '1-Jan-2013');
$data[] = array('id' => 8, 'name' => 'name 8', 'age' => 20, 'date' => '1-Feb-2013');

Put Data array into CArrayDataProvider and remember set the keyField inside it.
$dataProvider = new CArrayDataProvider(
	$data,
	array('keyField' => 'id')
);

After that in cgridview set the dataProvider to $dataProvider.
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
));

After that, try run your script will display as below.


Page you may also interest. Yii CGridView listdata with CArrayDataProvider and Pagination

Make a Comment

You must be logged in to post a comment.