select2 v4 dataAdapter.query没有触发

时间:2017-10-24 12:30:52

标签: jquery-select2-4

我有一个输入,我想为自定义查询绑定dataAdapter,如https://select2.org/upgrading/migrating-from-35#removed-the-requirement-of-initselection

中所述
<input name="pickup_point">

我的剧本:

Application.prototype.init = function() {
    this.alloc('$pickupLocations',this.$el.find('[name="pickup_point"]'));
    var that = this;
    $.fn.select2.amd.require([
        'select2/data/array',
        'select2/utils'
        ], function (ArrayData, Utils) {
            var CustomData = function($element, options) {
                CustomData.__super__.constructor.call(this, $element, options);
            };Utils.Extend(CustomData, ArrayData);
            CustomData.prototype.query = function (params, callback) {
                var data = {
                    results: []
                }; 
                console.log("xxx");
                for (var i = 1; i < 5; i++) {
                    var s = "";

                    for (var j = 0; j < i; j++) {
                        s = s + params.term;
                    }

                    data.results.push({
                        id: params.term + i,
                        text: s
                    });
                }

                callback(data);
            };

            that.$pickupLocations.select2({
                minimumInputLength: 2,
                language: translations[that.options.lang], 
                tags: [],
                dataAdapter: CustomData

            });

        });

}

但是当我在select2搜索框中输入xxx时,我正在测试的日志没有出现在我的控制台中。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案 https://github.com/select2/select2/issues/4153#issuecomment-182258515

问题是我试图在输入字段上初始化select2。

通过将类型更改为选择,一切正常。

<select name="pickup_point">
</select>