【问题标题】:Filter Gridview with Tag使用标签过滤 Gridview
【发布时间】:2017-09-20 16:28:39
【问题描述】:

查看代码:

 [
        'attribute' => 'name',
        'filterType'=>GridView:: FILTER_SELECT2,
        'filter'=>ArrayHelper::map(\app\models\Item::find()->orderBy('category')->asArray()->all(), 'name', 'name'),
        'filterWidgetOptions'=>[
            'pluginOptions'=>['allowClear'=>true, 'minimuminputLength' => 3],
        ],
        'filterInputOptions'=>['placeholder'=>'Any Item', 'multiple' => true ],
        'group'=>true,  // enable grouping
        'label' => 'Item',
        'contentOptions' =>
            ['style'=>'max-width: 150px; font-size: 12px;overflow: auto; word-wrap: break-word;'],

    ],

该代码有效,但标签值应该是从列表中选择的项目名称值,而我想要的是我可以为标签过滤器/搜索键入任何单词,而不是我应该从列表中选择值。 谢谢你

【问题讨论】:

    标签: gridview yii2 tags jquery-select2


    【解决方案1】:

    使用以下代码在网格视图中获取 TypeAhead 过滤器值:

         [
                'attribute' => 'attributeName',
                'format' => 'raw',
                'value' => function ($data) {
                    return$data->attributeName;
                },
                'filter' => Select2::widget([
                    'model' => $searchModel,
                    'attribute' => 'attributeName',
                    'data' => Model::getAllModelListFunction(),
                    'options' => [
                        'placeholder' => 'Select',
                    ],
                    'pluginOptions' => [
                        'allowClear' => true
                    ],
                ]),
            ],
    

    getAllModelFunction 应该是这样的:

    public static function getAllModelFunction()
    {
       $data = self::find()->select('id,name')->asArray()->all();
       return ArrayHelper::map($data, 'id', 'title');
    }
    

    记得在 Index.php 文件中安装和使用以下小部件:

      use kartik\select2\Select2;
    

    【讨论】:

    • 我应该把这个public static function getAllModelFunction() { $data = self::find()->select('id,name')->asArray()->all(); return ArrayHelper::map($data, 'id', 'title'); } 放在哪里?在控制器上?
    • 在各自的模型中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多