13

What is the correct way to do a row by row css expression. In Yii 1 is was rowCssClass. I couldn't figure out how to achieve this with Yii2. I had tried this, but wasn't sure I was on the right lines:

        'rowOptions' => function($model, $key, $index, $grid){
        if($data->option->correct_answer == 1){

            return ['class' => 'danger'];
        }
    },

I'm unsure where to get the parameters for the function from when dealing with the dataProvider though.

Jonnny
  • 4,939
  • 11
  • 63
  • 93
  • btw this is an excellent example how a framework can block your work. Doing this manually would take 5 seconds, trying to find out how to do this (in a horribly complicated way with Yii) took me 2 hours. Just saying :( – Sliq Mar 27 '15 at 16:00
  • 1
    @Sliq Could be clearer I would agree. I remember I did make the initial error though. Hopefully this answer/question will save people time though. – Jonnny Mar 28 '15 at 03:11

2 Answers2

28

Use $model instead $data.

In my variant:

   'rowOptions' => function ($model, $index, $widget, $grid){
      return ['style'=>'color:'.$model->status->color.'; background-color:'.$model->status->background_color.';'];
    },

In your case:

   'rowOptions' => function ($model, $index, $widget, $grid){

      if($model->option->correct_answer == 1){
        return ['class' => 'danger'];
      }else{
        return [];
      }
    },
user1852788
  • 4,278
  • 26
  • 25
  • @useruser1852788 I seem to get array_merge(): Argument #2 is not an array. – Jonnny Oct 15 '14 at 21:19
  • looks like you should return array, so I've edit the answer - added else part of "if" statement with empty array as return value. – user1852788 Oct 16 '14 at 06:22
  • Same thing, stack is this `foreach ($this->columns as $i => $column) { if (is_string($column)) { $column = $this->createDataColumn($column); } else { $column = Yii::createObject(array_merge([ 'class' => $this->dataColumnClass ? : DataColumn::className(), 'grid' => $this, ], $column)); }` – Jonnny Oct 16 '14 at 20:35
  • I thought we talk about rows... and class for rows. Your code from last comment is about columns. Show whole code of your widget call. – user1852788 Oct 17 '14 at 05:16
  • User1852788 it is about rows. The code is the stack error. – Jonnny Oct 17 '14 at 10:03
  • Thanks, it was my mistake, putting the rowOptions inside columns array. – Jonnny Oct 18 '14 at 13:57
0

You can also try this

add class name for your row

'rowOptions' => ['class'=>'rowData'],

then manipulate it through css

<?php

$css = <<< CSS
//example
.rowData:hover{

}
CSS;
$this->registerCss($css);
?>
zxKazumaxz
  • 13
  • 1
  • 7