i want to be able to style and change the value of data in a gridview. The styling works if i do
[‘attribute’ => ‘is_active’,
'header' => 'Active?',
'contentOptions'=>function($model, $key, $index, $column) {
if($model->is_active == Apiary::ACTIVE)
return ['style' => 'color:green'];
else
return ['style' => 'color:red'];
}
],
however, i also want to display ‘false’ instead of 0 (Apiary::ACTIVE=0), ‘true’ instead of 1, etc., i.e. if($model->is_active ==0) echo ‘False’;
how best do both apply style and conditionally change the value displayed in a gridview cell?
thanks
softark
(Softark)
October 20, 2017, 2:39am
2
thanks, that solves half the problem. However, it seems that if I use the $value parameter, then I can’t style the response. for example, if I have the below- how do i then color, bold, etc. the text that i am returning?- thanks:
['attribute' => ‘active,
'header' => 'Active',
'value'=>function($model) {
if($model->active ==0)
return ‘Not Active'; // how to color text?
elseif($model->active ==1)
return ‘Active'; // how to color text?
else
return ‘Transferred’; // how to color text?
}
],
softark
(Softark)
October 20, 2017, 2:57pm
4
It may not look very smart, but something like the following will do:
[
'attribute' => 'is_active',
'header' => 'Active?',
'contentOptions'=>function($model, $key, $index, $column) {
if($model->is_active == Apiary::ACTIVE)
return ['style' => 'color:green'];
else
return ['style' => 'color:red'];
},
'value'=>function($model) {
if($model->active ==0)
return ‘Not Active';
elseif($model->active ==1)
return ‘Active';
else
return ‘Transferred';
},
],
snathan
(snathan)
December 24, 2021, 1:28pm
6
Since Yii uses Bootstrap you are better of using a Bootstrap class rather than style as below:
'contentOptions' => function($model) {
if ($model->taskduedt < date("Y-m-d")) {
return ['class' => 'table-danger',];
} elseif ($model->taskduedt == date("Y-m-d")) {
return ['class' => 'table-warning',];
}
else return ['class' => '',];
},