【问题标题】:Add checkbox in Yii2 GridView filtering在 Yii2 GridView 过滤中添加复选框
【发布时间】:2018-08-22 17:50:03
【问题描述】:

我正在使用 Yii2,我想在 GridView 搜索中为布尔变量添加一个复选框过滤器。这是我来自ModelSearch的规则:

public function rules()
{
     return [
         [['bool1','bool2','bool3','bool4'],'boolean']
     ];
}

那么,我怎样才能呈现为复选框而不是文本输入?

这些是我的GridView 参数:

$paramsCustom = [
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            ['class' => 'yii\grid\CheckboxColumn'],
            [ 
                //boolean column
                'attribute' => 'bool1',
                'label' => 'S',
                'format' => 'raw',
                'value' => function ($model, $index, $widget) {
                     ....           
                },
            ],

我的bool1 属性是布尔值。因此,在GridView 过滤中出现了一个文本输入,我想使用复选框而不是文本输入来过滤GridView 中显示的结果。

这是我的GridView 专栏:

【问题讨论】:

  • 到目前为止你做了什么..
  • 什么?我不关注你..
  • 你能给我们看看你做的一些代码吗..
  • 问题已编辑。
  • 感谢 Nitin Pund。使用下拉列表解决: [ 'attribute' => 'bool1', 'label' => 'W', 'format' => 'html', 'value' => function ($model, $index, $widget) { ... }, '过滤器' => [ '0' => '是', '1' => '否', ] ],

标签: yii2


【解决方案1】:

使用复选框进行过滤通常没有多大意义,因为复选框只能表示两种状态:选中或未选中。但是,对于过滤,您实际上需要三种状态:

  1. 仅选中,
  2. 仅未选中,
  3. 全部(无过滤)。

在这种情况下,您可能应该使用下拉菜单:

[ 
    'attribute' => 'bool1',
    'label' => 'S',
    'format' => 'raw',
    'value' => function ($model, $index, $widget) {
         ....           
    },
    'filter' => [1 => 'Yes', 0 => 'No'], 
],

它将生成三个选项:“是”、“否”和用于禁用过滤的空默认位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多