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.

2
  • 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 :( Commented Mar 27, 2015 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. Commented Mar 28, 2015 at 3:11

2 Answers 2

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 []; } }, 
Sign up to request clarification or add additional context in comments.

6 Comments

@useruser1852788 I seem to get array_merge(): Argument #2 is not an array.
looks like you should return array, so I've edit the answer - added else part of "if" statement with empty array as return value.
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)); }
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 it is about rows. The code is the stack error.
|
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); ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.