Search code examples
gridviewfilteryii2widget

Yii2: How to add the All option to the GridView filter?


I have a GridView with a filter of my index.php file made with Yii2. It shows this options:

  • blank
  • No

Sí and No means Yes and No in Spanish. I need to show the world All instead of the blank option:

GridView showing the filter (options: blank, yes and no)

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        'nombre_motivo_movimiento',
        [
            'attribute' => 'entrada',
            'format' => 'raw',
            'filter' => ['1' => 'Sí', '0' => 'No'], // The filter I want to improve.
            'contentOptions' => [
                'class' => 'text-center',
            ],
            'value' => function($m){
                if ($m->entrada) {
                    return '<span class="label label-success"><i class="fa fa-check"></span>';
                }
                else {
                    return '<span class="label label-danger"><i class="fa fa-remove"></span>';
                }
            }
        ],
        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '{update} {delete}'
        ],
    ],
]) ?>

Solution

  • add the property 'filterInputOptions':

    'filter' => ['1' => 'Sí', '0' => 'No'], // The filter I want to improve.
    'filterInputOptions' => ['class' => 'form-control', 'id' => null, 'prompt' => 'Todos'], // to change 'Todos' instead of the blank option