F
F
FrelFrloich2019-02-23 18:33:05
Yii
FrelFrloich, 2019-02-23 18:33:05

Yii2 gridview how to assign class under different conditions?

Good day, there is a grid, I would like to tritely assign a class to a certain line in the grid,

[
   'attribute' => 'date',
  'contentOptions' => ['class' => 'date'],
],

everything is clear with this, but how to build a condition in the grid, I need to assign a different class if the date is less than today ... It can be done in the grid itself; if so, how is the if construct used in gridview?)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FrelFrloich, 2019-02-23
@FrelFrloich

To whom MB is useful, the right decision.

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'rowOptions' => function ($model, $key, $index, $grid)
        {
          if($model->date < date('Y-m-d')) {
              return ['class' => 'red'];
          }
        },

Condition and everything else to your taste.

Y
Yuri Esin, 2019-02-23
@Exomode

$data = [
   'attribute' => 'date',
  'contentOptions' => ['class' => 'date'],
];

if ($yourTime < time()) {
    $data["contentOptions"]["class"] = "otherDate";
}

M
morricone85, 2019-02-23
@morricone85

'contentOptions' => function () {
    return ['class' => 'myClass'];
},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question