Changing the background color in a gridview cell Yii2 depends on its value

I am trying to make a background color that depends on the calculation number of the value in one cell. This is my code:

[
'attribute' => 'coefTK',
'label' => '<abbr title="Koefisien Jumlah Tenaga Kerja">TK</abbr>',
'encodeLabel' => false,
'headerOptions' => ['style'=>'text-align:center'],
'options' => [ 'style' => $dataProvider['coefTK']/$dataProvider['coefTK_se']<2 ? 'background-color:red':'background-color:blue'],
        ],

but I got an error that says: "You cannot use an object like yii \ data \ ActiveDataProvider as array"

So, how can I change the background grid cell that matters from some calculation?

+4
source share
1 answer

Use contentOptions:

[
    'attribute' => 'coefTK',
    'label' => '<abbr title="Koefisien Jumlah Tenaga Kerja">TK</abbr>',
    'encodeLabel' => false,
    'headerOptions' => ['style'=>'text-align:center'],
    'contentOptions' => function ($model, $key, $index, $column) {
        return ['style' => 'background-color:' 
            . (!empty($model->coefTK_se) && $model->coefTK / $model->coefTK_se < 2
                ? 'red' : 'blue')];
    },
],
+4
source

Source: https://habr.com/ru/post/1654411/


All Articles