0
votes

I'm using Yii2 and I want to add a checkbox filter for a boolean variables in a GridView search. This is my rules from ModelSearch:

public function rules()
{
     return [
         [['bool1','bool2','bool3','bool4'],'boolean']
     ];
}

So, how can I render as a checkbox instead of text input?

These are my GridView parameters:

$paramsCustom = [
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            ['class' => 'yii\grid\CheckboxColumn'],
            [ 
                //boolean column
                'attribute' => 'bool1',
                'label' => 'S',
                'format' => 'raw',
                'value' => function ($model, $index, $widget) {
                     ....           
                },
            ],

My bool1 attribute is boolean. So, in the GridView filtering appears a text input, and I want to filter the results displayed in the GridView using a checkbox instead a text input.

This is my GridView column:

This is my GridView column

1
what you have done so far.. - Nitin Pund
What? I don't follow you.. - opare
can you show us some code that you did.. - Nitin Pund
Question edited. - opare
Thanks Nitin Pund. Solved using a dropdown: [ 'attribute' => 'bool1', 'label' => 'W', 'format' => 'html', 'value' => function ($model, $index, $widget) { ... }, 'filter' => [ '0' => 'Yes', '1' => 'No', ] ], - opare

1 Answers

2
votes

Using checkbox for filtering usually does not make much sense, since checkbox can represent only two states: checked or unchecked. However for filtering you actually need three states:

  1. only checked,
  2. only unchecked,
  3. all (no filtering).

You should probably use dropdown in this case:

[ 
    'attribute' => 'bool1',
    'label' => 'S',
    'format' => 'raw',
    'value' => function ($model, $index, $widget) {
         ....           
    },
    'filter' => [1 => 'Yes', 0 => 'No'], 
],

It will generate three options: "Yes", "No" and empty default position for disabling filtering.