1

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

6
  • what you have done so far.. Commented Aug 22, 2018 at 9:01
  • What? I don't follow you.. Commented Aug 22, 2018 at 9:14
  • can you show us some code that you did.. Commented Aug 22, 2018 at 9:21
  • Question edited. Commented Aug 22, 2018 at 9:31
  • 1
    Thanks Nitin Pund. Solved using a dropdown: [ 'attribute' => 'bool1', 'label' => 'W', 'format' => 'html', 'value' => function ($model, $index, $widget) { ... }, 'filter' => [ '0' => 'Yes', '1' => 'No', ] ], Commented Aug 22, 2018 at 9:42

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.