Reputation: 5133
I have a data provider which generates content like:
array(
[0] => ParentObjects array(
Child1Object array(...),
Child2Object array(...),
)
)
The first level of this array will always be 1. I want to show in a CGridView the content of both childs.
$dataProvider=new CActiveDataProvider('Campanii', array(
'criteria'=>array(
'with'=>array('stocs','produse'),
),
'pagination'=>array(
'pageSize'=>20,
),
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
array('name'=>'Cod','value'=>'$data->stocs[0]->cod_produs'),
),
));
As you see, the column cod
has the value array('name'=>'Cod','value'=>'$data->stocs[1]->cod_produs')
. Notice that [0]. Instead of that zero, I want something like an iterator. In this case, it shows me the content of that attributes in a column.
Is this possible?
Upvotes: 0
Views: 280
Reputation:
Yes, you can write any complex expression you want. However, for readability it is better to use an actual function. This example assumes PHP 5.3+, if your environment doesn't support that, just create a normal function instead of an anonymous one (or find a better environment).
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
array(
'name' => 'Cod',
// needed when returning HTML instead of plain text
'type' => 'raw',
// this function is called each time a cell needs to be rendered
'value' => function(Companii $data) {
$codes = array();
foreach($data->stocs as $stoc) {
$code = CHtml::encode($stoc->cod_produs);
$codes []= CHtml::tag('li', array(), $code);
}
$codes = implode("\n", $codes);
return CHtml::tag('ul', array(), $codes);
},
),
),
));
Upvotes: 1