rowOptions for Gridview

Hi

I’m trying to add a particular class to each row in my grid depending on the outcome of an if statement.




    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'columns' => [

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


              if($model->option->correct_answer == 1){

                return ['class' => 'success'];

              } else {

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

              }

        },



But I always get array_merge(): Argument #2 is not an array in my stack on GridView.php




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)); // here



I looked at Cebe’s answer here and I thought my attempt was ok here https://github.com/yiisoft/yii2/issues/2588

What have I got wrong here ???

rowOptions is contained by array columns in your code. I think you have to declare it this way:




    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'columns'      => [...],

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

            ...

        },



Can you try this?

if you take a closer look you have a syntax error





<?= GridView::widget([

        'dataProvider' => $dataProvider,

        'columns' => [ 

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


              if($model->option->correct_answer == 1){

                return ['class' => 'success'];

              } else {

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

              }

        },


// move the callback/rowOptions above columns array like so

<?= GridView::widget([

        'dataProvider' => $dataProvider,

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

              if ($model->option->correct_answer == 1) {

                return ['class' => 'success'];

              } else {

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

              }

        },

        'columns' => [

         // your coulmns go here 

        ...

       ]



:rolleyes:

Thanks, that drove me mad.

no worries happens to most of us at least to me