Yii2 Gridview row by row css expression

2019-03-15 08:23发布

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.

标签: gridview yii2
2条回答
可以哭但决不认输i
2楼-- · 2019-03-15 08: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 [];
      }
    },
查看更多
相关推荐>>
3楼-- · 2019-03-15 08:39

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);
?>
查看更多
登录 后发表回答