【发布时间】:2018-11-30 04:47:18
【问题描述】:
我有一个 Yii2 应用程序,在我的一个索引视图(使用 gii cli 工具生成的默认 crud 的修改版本)上,我已将 GridView 小部件替换为 Kartik 小部件,并且同样将一列设置为使用filterType 的GridView::FILTER_SELECT2
我的问题是,当将数组传递给没有filterType 的列时,我会得到一个选择菜单,其中有一个空白选项可以“清除”搜索过滤器:
[
'attribute' => 'scale_id',
'label' => 'Scale',
'value' => function($model) {
return empty($model->scale) ? null : $model->scale->name;
},
'filter' => ArrayHelper::map(Scale::find()->asArray()->all(), 'id', 'name'),
],
但是,通过将过滤器更改为 Kartik 的 select2 一个,不会出现空选项并且相同的行为不适用:
[
'attribute' => 'scale_id',
'label' => 'Scale',
'value' => function($model) {
return empty($model->scale) ? null : $model->scale->name;
},
'filter' => ArrayHelper::map(Scale::find()->asArray()->all(), 'id', 'name'),
'filterType' => GridView::FILTER_SELECT2,
],
如何使用 Kartik 的 select2 过滤器实现相同的“除非更改为空白”选择过滤器?
更新:
将prompt 与allowClear 结合使用可重新创建类似的功能,但仍不理想。初始显示如下所示:
但是,一旦选择了选项,关闭 x 将不适合并覆盖文本,并且不提供在下拉列表中具有空白/空值的原始行为:
这是我的GridView代码
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'id',
'description',
'sku_number',
[
'attribute' => 'owner_id',
'label' => 'Owner',
'value' => function($model) {
return $model->owner->name;
},
'filter' => ArrayHelper::map(Owner::find()->asArray()->all(), 'id', 'name'),
'filterType' => GridView::FILTER_SELECT2,
'filterWidgetOptions' => [
'options' => ['prompt' => ''],
'pluginOptions' => ['allowClear' => true],
],
],
[
'attribute' => 'product_id',
'label' => 'Product',
'value' => function($model) {
return $model->product->name;
},
'filter' => ArrayHelper::map(Product::find()->asArray()->all(), 'id', 'name'),
'filterType' => GridView::FILTER_SELECT2,
'filterWidgetOptions' => [
'options' => ['prompt' => ''],
'pluginOptions' => ['allowClear' => true],
],
],
[
'attribute' => 'manufacturer_id',
'label' => 'Manufacturer',
'value' => function($model) {
return $model->manufacturer->name;
},
'filter' => ArrayHelper::map(Manufacturer::find()->asArray()->all(), 'id', 'name'),
'filterType' => GridView::FILTER_SELECT2,
'filterWidgetOptions' => [
'options' => ['prompt' => ''],
'pluginOptions' => ['allowClear' => true],
],
],
[
'attribute' => 'scale_id',
'label' => 'Scale',
'value' => function($model) {
return empty($model->scale) ? null : $model->scale->name;
},
'filter' => ArrayHelper::map(Scale::find()->asArray()->all(), 'id', 'name'),
'filterType' => GridView::FILTER_SELECT2,
'filterWidgetOptions' => [
'options' => ['prompt' => ''],
'pluginOptions' => ['allowClear' => true],
],
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
【问题讨论】:
-
我认为你需要使用
pluginOptions下的"allowClear"=>true作为select2 -
@MuhammadOmerAslam 我试过了(并在其他 select2 小部件中使用它)但它不会让它清楚,因为没有 null/clear 值。稍后我会尝试将其与
prompt结合使用。 -
您需要提供
prompt或placeholder以使其清除,因为它需要一个空白选项才能重置为 -
@MuhammadOmerAslam 结合了
prompt和allowClear确实使它工作了,尽管x被压扁了。自动调整表格大小效果不佳,因此我需要进行调整。有没有办法像默认一样从下拉列表中选择空白条目? -
你能显示当前显示器的屏幕抓取吗,据我所知,如果你指定宽度自动,它将根据占位符的长度或选定的值/选项扩展