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

我想Gridview用一个Select2按钮(允许用户手动编写选项的下拉按钮)更改内置搜索框过滤器。我使用两个合并表,并且wp_id是两个表中的列,并且user_id是Accounts模型中的列。使用此处使用的代码,我在我的 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,

                            ],

                        ]),

            ],

        ],

]

) ?>

我在这里想念什么?


幕布斯6054654
浏览 165回答 1
1回答

缥缈止盈

因为这里没有$form您尝试使用的任何内容,您应该直接使用小部件初始化 select2 并指定 select2 小部件的attribute和model属性。请参阅下面的代码应该是什么样子。<?php&nbsp; &nbsp; echo GridView::widget(&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'dataProvider' => $dataProvider,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filterModel' => $searchModel,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'columns' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'attribute' => 'wp_id',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value' => 'accounts.user_id',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filter' => Select2::widget(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'model' => $searchModel,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'attribute' => 'wp_id',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'data' => ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'options' => ['placeholder' => ' --Filter by user id-- '],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'language' => 'en',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'pluginOptions' => [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'allowClear' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; ]);
打开App,查看更多内容
随时随地看视频慕课网APP