简体   繁体   English

Yii2:Gridview 过滤器中的 kartik\\Select2 下拉列表

[英]Yii2: kartik\Select2 dropdown list in Gridview filter

I want to change the inbuilt search box filter in a Gridview with a Select2 button (dropdown button which allows the user to write the option manually).我想使用Select2按钮(允许用户手动编写选项的下拉按钮)更改Gridview中的内置搜索框过滤器。 I use two merged tables and wp_id is a column in both tables, and user_id is a column in the Accounts model.我使用了两个合并表, wp_id是两个表中的一列,而user_idAccounts模型中的一列。 With the code used here, I see on my Grid a simple text input the default one.使用此处使用的代码,我在我的 Grid 上看到一个简单的文本输入默认文本。

<?php echo GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'attribute' => 'wp_id',
                'value' => 'accounts.user_id',
                'filter' => $form->field($searchModel, 'wp_id')
                    ->widget(
                        Select2::className(),
                        [
                            'data' => ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id'),
                            'options' => ['placeholder' => ' --Filter by user id-- '],
                            'language' => 'en',
                            'pluginOptions' => [
                                'allowClear' => true,
                            ],
                        ]),
            ],
        ],
]
) ?>

What am i missing here?我在这里缺少什么?

Because there isn't any $form here that you are trying to use, you should initialize the select2 directly using the widget and specify the attribute and model properties of the select2 widget.因为这里没有您尝试使用的任何$form ,所以您应该直接使用小部件初始化 select2 并指定 select2 小部件的attributemodel属性。

See below how your code should look like.请参阅下面的代码。

<?php

    echo GridView::widget(
        [
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                [
                    'attribute' => 'wp_id',
                    'value' => 'accounts.user_id',
                    'filter' => Select2::widget(
                        [
                            'model' => $searchModel,
                            'attribute' => 'wp_id',
                            'data' => ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id'),
                            'options' => ['placeholder' => ' --Filter by user id-- '],
                            'language' => 'en',
                            'pluginOptions' => [
                                'allowClear' => true,
                            ],
                        ]
                    ),
                ],
            ],
        ]
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM