首页 > 解决方案 > 在复选框列 Yii2 Gridview 小部件上添加过滤器

问题描述

您能帮我在网格视图小部件 yii2 上的复选框列上添加过滤器吗?我使用 yii\grid\CheckboxColumn 在我的 index.php 的 Gridview 中添加复选框列。但我无法在列上方添加过滤器作为其他列。请在下面查看我的代码。

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',
                            ]);
                        }
                    ],
                ],
            ],
        ]);

以下是复选框列。

['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];
                    }
                ],

有人可以帮忙吗?

标签: phpyii2

解决方案


你可以使用你的 Gii 工具-> CRUD 生成器来创建你的过滤器文件。然后你可以像这样将你的参数传递给搜索模型:

$searchModel = 新搜索模型;

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

您需要从 SearchModel 返回您的 $dataProvider


推荐阅读