A
A
AlexAll2019-01-08 17:03:42
Yii
AlexAll, 2019-01-08 17:03:42

How to remove empty option in gridview in format row in yii2?

In the gridview, I made a drop-down list with which you can display published and unpublished posts.

[
                'attribute' => 'status', 
                'format' => 'raw',
                'filter' => [
                    'off' => 'Не опубликована',
                    'on' => 'Опубликована',
                ],
                'headerOptions' => ['width' => '200'],
                'value' => function ($model, $key, $index, $column) {
                    $status = $model->status === 'on';
                    return \yii\helpers\Html::tag(
                        'span',
                        $status ? 'Опубликована' : 'Не опубликована',
                        [
                            'class' => 'label label-' . ($status ? 'success' : 'danger'),
                            
                        ]
                    );
                },
                
            ],

And in this drop-down list, an empty option will also drop out
<option value=""></option>
<option value="off" selected>Не опубликована</option>
<option value="on">Опубликована</option>

How to exclude it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2019-01-08
@webinar

You can pass not an array, but a string in which there will be ready-made html for the filter cell, and there you can do whatever you want. If you are interested in how it works, then here are the sources:
https://github.com/yiisoft/yii2/blob/master/framew...
Alternatively, you can create your own yii\grid\DataColumn and remake this method as you wish.
But it seems to me that the goal itself is not quite adequate. You display all values ​​in the table, which means that there is no filter set for them, so there should be an empty option. You will not be able to select all entries will have or published or not. Why take functionality away from the user?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question