【问题标题】:Select2 multiple initial pre-selected optionsSelect2 多个初始预选选项
【发布时间】:2015-05-06 14:49:33
【问题描述】:

我正在尝试让 select2 使用多个预选选项。我正在使用它让用户选择几个选项,然后将它们保存到然后能够返回并编辑它们。但是我无法让 select2 initSelection 正常工作以填充用户已经选择的现有选项。这是我尝试与警报一起使用的代码,以显示何时触发了警报(“完成”);尚未触发,并且 alert($(element).val(0) 触发了两次。一次使用输入中的值,然后再次使用空白。

<input type="text" id="Commissioners" name="Commissioners" value="test" />

$("#Commissioners").select2({
                placeholder: 'Select Commissioners',
                minimumInputLength: 2,
                allowClear: true,
                multiple: true,
                width: 'resolve',
                ajax: {
                    url: "/Commissioner/CommissionerAutoComplete/",
                    dataType: 'json',
                    data: function (term, page) {
                        return {
                            search: term // search term
                        };
                    },
                    results: function (data) {
                        return { results: data };
                    }
                },
                initSelection: function (element, callback) {
                    // the input tag has a value attribute preloaded that points to a preselected make's id
                    // this function resolves that id attribute to an object that select2 can render
                    // using its formatResult renderer - that way the make text is shown preselected
                    alert($(element).val());
                    $.ajax({
                        url: "/UserProfile/LoadServiceTypeAndCommissioners",
                        type: "GET",
                        dataType: "json",
                        data: { UserId: '@Model.UserID', serviceTypeId: id}                            
                    }).done(function (data) {
                        alert("done");
                        callback(data.results);
                    });

                },
                formatResult: s2FormatResult,
                formatSelection: s2FormatSelection
            }).select2('val', []);



I figured out that the second alert fires for the .select2('val', []); at the end. however removing this causes the placeholder to not work and adding in a value here like ["1"] does not result in a selected item

【问题讨论】:

    标签: javascript jquery jquery-select2


    【解决方案1】:

    事实证明这是导致它无法工作的多种因素的组合。我没有将 ajax 调用中的代码作为 json 对象返回,但仅此一项并不能解决它。我必须在成功的回调中向 ajax 调用添加成功和错误选项。奇怪的是,我仍然需要保留 .done 及其回调。

    $("#Commissioners").select2({
                    placeholder: 'Select Commissioners',
                    minimumInputLength: 2,
                    allowClear: true,
                    multiple: true,
                    width: 'resolve',
                    ajax: {
                        url: "/Commissioner/CommissionerAutoComplete/",
                        dataType: 'json',
                        data: function (term, page) {
                            return {
                                search: term // search term
                            };
                        },
                        results: function (data) {
                            return { results: data };
                        }
                    },
                    initSelection: function (element, callback) {
                        // the input tag has a value attribute preloaded that points to a preselected make's id
                        // this function resolves that id attribute to an object that select2 can render
                        // using its formatResult renderer - that way the make text is shown preselected
                        //alert($(element).val());
                        $.ajax({
                            url: "/UserProfile/LoadServiceTypeAndCommissioners",
                            type: "GET",
                            dataType: "json",
                            data: { UserId: '@Model.UserID', serviceTypeId: id },
                            success: function (data) {                                
                                callback(data);
                            },
                            error: function (data) {
    
                            }
                        }).done(function (data) {                            
                            callback(data.results);
                        });
    
                    },
                    formatResult: s2FormatResult,
                    formatSelection: s2FormatSelection
                }).select2('val', ["1"]);
    

    【讨论】:

      【解决方案2】:

      这是我在这里看到的问题: $.ajax 调用需要时间来接收数据,select2 构造函数不会等待 initSelection 设置。

      尝试将 ajax 调用从 select2 初始化移到一个函数,并在 select2 初始化后立即调用它

      【讨论】:

      • 然后会填充初始值吗?是不是 initselection 这样做的重点是什么?
      • 如果你想用值初始化 select2,而不是先通过 ajax 调用获取它们,在成功调用 ajax 后(例如在success 回调中),用你的值初始化你的 select2
      猜你喜欢
      • 2017-04-06
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多