1

Can you help me to add a filter on checkbox column on Grid view widget yii2? I used yii\grid\CheckboxColumn to add the check box column in the Gridview on my index.php. But I couldn't add a filter above the column as the other columns. Please look into my code below.

GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
                'id',
                ['label' => 'Client', 'attribute' => 'name', 'value' => 'client.view', 'format' => 'raw'],
                'postCode',
                'start',
                'end',
                [
                    'attribute' => 'categoryID',
                    'value' => 'category.name',
                    'filter' => ArrayHelper::map(Servicecategory::find()->where(['status' => true])->asArray()->all(), 'id', 'name')
                ],
                [
                    'attribute' => 'status',
                    'headerOptions' => ['style' => 'width:12%'],
                    'value' => 'Status',
                    'filter' => array_filter(\app\models\Booking::$statuses),
                    'filterInputOptions' => ['class' => 'form-control', 'prompt' => 'All']
                ],
                ['class' => 'yii\grid\CheckboxColumn',
                    'header' => 'follow Up',
                    'contentOptions' => ['class' => 'text-center'],
                    'checkboxOptions' => function($model, $key, $index) {
                        $url = \yii\helpers\Url::to(['booking/followup/' . $model->id]);
                        return ['onclick' => 'js:followUp("' . $url . '")', 'checked' => $model->followUpEmailSent ? true : false, 'value' => $model->followUpEmailSent];
                    }
                ],
                ['class' => 'yii\grid\ActionColumn',
                    'headerOptions' => ['style' => 'width:10%'],
                    'template' => '{view} {approval} {update} {delete} ',
                    'buttons' => [
                        /* 'view' => function ($url, $model) {
                          return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', ['/booking/review/' . $model->id], [
                          'title' => Yii::t('app', 'Review'),
                          ]);
                          }, */
                        'approval' => function ($url, $model) {
                            return Html::a('<span class="glyphicon glyphicon-ok"></span>', ['/booking/approval/' . $model->id], [
                                        'title' => Yii::t('app', 'Additional Details'),
                                        'class' => 'error',
                            ]);
                        }
                    ],
                ],
            ],
        ]);

Following is the checkbox column.

['class' => 'yii\grid\CheckboxColumn',
                    'header' => 'follow Up',
                    'contentOptions' => ['class' => 'text-center'],
                    'checkboxOptions' => function($model, $key, $index) {
                        $url = \yii\helpers\Url::to(['booking/followup/' . $model->id]);
                        return ['onclick' => 'js:followUp("' . $url . '")', 'checked' => $model->followUpEmailSent ? true : false, 'value' => $model->followUpEmailSent];
                    }
                ],

Can someone help with this?

2 Answers 2

1

You may use your Gii tool-> CRUD generator to create your filter file. Then you can pass your params to search model like this:

$searchModel = new SearchModel;

$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

You will need to return your $dataProvider from SearchModel

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

Comments

-1

Use the search file for implement filters on grid.

I suggest that do not use gridview filter in your case.

Write your search related code in search file and add checkbox on search form.

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>

<div class="ideas-search">

<?php $form = ActiveForm::begin([
    'action' => ['index'],
    'method' => 'get',
]); ?>

<?php echo $form->field($model, 'name[]')->checkboxList(
        ['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']); 
?>

<div class="form-group">
    <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
    <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>

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.