Yii2 Gridview row by row css expression

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-31 21:27:17

问题


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.


回答1:


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 [];
      }
    },



回答2:


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);
?>


来源:https://stackoverflow.com/questions/26372531/yii2-gridview-row-by-row-css-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!