【问题标题】:Yii2 Kartik Gridview + Select2 filterYii2 Kartik Gridview + Select2 过滤器
【发布时间】:2018-11-30 04:47:18
【问题描述】:

我有一个 Yii2 应用程序,在我的一个索引视图(使用 gii cli 工具生成的默认 crud 的修改版本)上,我已将 GridView 小部件替换为 Kartik 小部件,并且同样将一列设置为使用filterTypeGridView::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 过滤器实现相同的“除非更改为空白”选择过滤器?

更新:promptallowClear 结合使用可重新创建类似的功能,但仍不理想。初始显示如下所示: 但是,一旦选择了选项,关闭 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"=&gt;true作为select2
  • @MuhammadOmerAslam 我试过了(并在其他 select2 小部件中使用它)但它不会让它清楚,因为没有 null/clear 值。稍后我会尝试将其与prompt 结合使用。
  • 您需要提供promptplaceholder 以使其清除,因为它需要一个空白选项才能重置为
  • @MuhammadOmerAslam 结合了promptallowClear 确实使它工作了,尽管x 被压扁了。自动调整表格大小效果不佳,因此我需要进行调整。有没有办法像默认一样从下拉列表中选择空白条目?
  • 你能显示当前显示器的屏幕抓取吗,据我所知,如果你指定宽度自动,它将根据占位符的长度或选定的值/选项扩展

标签: gridview yii2


【解决方案1】:

您需要将pluginOptions 下的"allowClear"=&gt;true 选项用于Select2 以及"prompt"=&gt;''

要调整列大小,请尝试将filterInputOptions 用于id,并使用manufacturer_id 调整它们的宽度,因为它们占用的空间超过了所需的空间并且 select2 被挤压。

[
    'attribute' => 'id',
    'filterInputOptions' => ['class' => 'form-control', 'style' => 'width:50px'],
],
[
    'attribute' => 'manufacturer_id',
    'filterInputOptions' => ['class' => 'form-control', 'style' => 'width:50px'],
],
[
    'attribute' => 'scale_id',
    'filter' => ArrayHelper::map(Scale::find()->asArray()->all(), 'id', 'name'),
    'filterType' => GridView::FILTER_SELECT2,
    'filterWidgetOptions' => [
        'options' => ['prompt' => ''],
        'pluginOptions' => [
            'allowClear' => true,
            'width'=>'resolve'
        ],
    ],
],

更新

确保GridView::FILTER_SELECT2 的默认宽度更好。将pluginOptions['width'] 设置为default 以解决。我已经为scale_id 更新了上面的代码,请参阅here

更新 2

显然,它不适用于 resolve,因为它会使 select2 的宽度变宽,您可能需要像 'width' =&gt; '200px' 一样手动设置它

【讨论】:

  • 仅设置 id 宽度是不够的,但我会调整大小以尝试使其正确。
  • 不好,这会导致一列变得超宽(1000%?)并推离屏幕
  • @sharf hmm... 我刚试过它看起来确实很奇怪,但如果你在这里提供手动宽度,比如'width' =&gt; '200px',,我认为它应该相应调整
【解决方案2】:

将您的数据列更改为

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

请联系我们filterWigetOptionsDataColumn

【讨论】:

  • 这让我从一个空白输入开始,这是一个很大的帮助,但一旦它被选中,我就不会清除输入。稍后我会尝试将其与allowClear 结合使用。
  • promptallowClear 结合是可行的,尽管它并不理想。如果您可以更新您的答案,我会接受。
猜你喜欢
  • 2020-04-14
  • 2018-09-01
  • 2017-10-16
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多