Last Updated: February 25, 2016
·
1.879K
· aspirinchaos

how to use CArrayDataProvider

CArrayDataProvider need for cases when you want pass an array to CGridView.

Key id must be in an array or it will not work.

$fruits = array(  
  array('id' => 1, 'name'=>'apple', 'color' => 'green'),  
  array('id' => 2, 'name'=>'orange', 'color' => 'orange'),  
  array('id' => 3, 'name'=>'banana', 'color' => 'yellow'),  
  array('id' => 4, 'name'=>'pineapple', 'color' => 'brown')  
);  

$dataProvider = new CArrayDataProvider($fruits);  

$this->widget('zii.widgets.grid.CGridView', array(  
  'id' => 'fruits-grid',  
  'dataProvider' => $dataProvider ,  
  'columns' => array(  
    array(  
        'name' => 'ID',  
        'value' => '$data["id"]',  
    ),  
    array(  
        'name' => 'Name',  
        'value' => '$data["name"]'  
    ),  
    array(  
        'name' => 'Color',  
        'value' => '$data["color"]'  
    ),  
  )  
));