简体   繁体   中英

Add filter on checkbox column Yii2 Gridview widget

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?

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM