How to use an element of json data instead of field in the widget to display full list?

213 Views Asked by At

I saved some data in json format into my database in one field called json. I used widget in order to display full list of products.

 <?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'page-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'name',
        'json',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

I decoded field (called json) which contains json data. And I am going to use a value of decoded json(p_1 element of decoded json (json["p_1"])) in the widget instead of whole field (called json).

(Because fields(called json) contains a lot of data and I need only one data). How can I use p_1 instead of field(called json)?

1

There are 1 best solutions below

2
On

You can pass a php expression as the value to be displayed. $data represents the data model for the row.

'columns' => array(
    'id',
    'name',
    array(
        'name' => 'json',
        'value' => '$data->json["p_1"]'
    ),

Reference: http://www.yiiframework.com/doc/api/1.1/CDataColumn#value-detail