Formatting gridView column field content

Hi everyone!

Below is my code.

product/index.php




<?= GridView::widget([

    'dataProvider' => $dataProvider,

    'filterModel' => $searchModel,

    'columns' => [

		  ['class' => 'yii\grid\SerialColumn'],


			[

			  'label'=>'Selling Price',

			  'attribute'=>'prod_sell_price',

			  'format'=>['decimal',2],

			],

			// 'prod_compare_price',

			'prod_active',

		  

                 ['class' => 'yii\grid\ActionColumn'],

		],

]); ?>



Would like to know :

  1. Can we use some condition in gridview? For example: instead of showing prod_active (1 or 0), can we make it like if prod_active == 1, echo ‘Selling’ in the field, if prod_active == 0, echo ‘On-Hold’ in the field?

  2. Can we make selling price column showing something like 13.00 10.00 (prod_compare_price prod_sell_price)?

Thank you in advance ;)

Check the ‘value’ property of DataColumn:

http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$value-detail

Hi,

May be this is an example.





[

            'attribute' => 'prod_active',//Header will be used based on the attribute or you customize

            'value' => function ($model, $key, $index, $column) {

                return $model->prod_active == 1 ? 'Selling' : 'On-Hold';

            },

            'format' => 'raw'

        ],



1 Like