首页 > 解决方案 > 两次调用模板选择(Select2 Kartik-yii2)

问题描述

我是一个新的Yii2。我有这个错误,我不知道如何解决这个问题。

Bug 很简单,templateSelection 调用了两次。这是我的代码。

如果我在 select2 中提交某些内容,则 templateSelection 会调用两次。

echo Select2::widget([
                            'name' => 'blogId',
                            'options' => ['placeholder' => 'Search for [![enter image description here][1]][1] ...'],
                            'pluginOptions' => [
                                'allowClear' => true,
                                'minimumInputLength' => 1,
                                'ajax' => [
                                    'url' => Url::to(['/controller/action']),
                                    'dataType' => 'json',
                                    'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                    'processResults' => new JsExpression($resultsJs),
                                ],
                                'templateResult' => new JsExpression($formatRepo),
                                'templateSelection' => new JsExpression('function(params) { console.log("tst");}'),
                            ],

                        ]);

谢谢您的帮助。

标签: phpyii2jquery-select2kartik-v

解决方案


我通过调试 select2.full.js 找到了自己的响应

问题就在这里。只需删除此代码。它调用了两次,因为它是嵌套函数。

    this.$element.on('change.select2', function () { self.dataAdapter.current(function (data) { 
//self.trigger('selection:update', { 
data: data }); 
// }); 
});

推荐阅读