简体   繁体   English

如何混合选择2使用初始选择加载远程数据

[英]How to mix Select 2 Loading Remote Data with Initial Selections

https://select2.github.io/examples.html#data-ajax https://select2.github.io/examples.html#data-ajax

For select2, I wanna: 对于select2,我想:

  1. List some initial selections, so for small list, we can select it directly. 列出一些初始选择,因此对于较小的列表,我们可以直接选择它。
  2. Search remote for long/more selections. 远程搜索长/更多选择。

But, it seems I cannot provide both ajax and data parameter at same time. 但是,似乎我无法同时提供ajaxdata参数。

Any suggestion? 有什么建议吗? Thanks. 谢谢。


code snippet added 添加了代码段

function installShopSelect2() {
    var url = "/ajax/brandEnterprise/findShops.mapi";
    "use strict";
    $('#shopid').select2({
        ajax: {
            type: "GET",
            url: url,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            delay: 250,
            //async: false,
            data: function (params) {
                return {
                    keyword: params.term,
                    besId: selectedBes,
                    page: params.page
                };
            },
            processResults: function (data, params) {
                // parse the results into the format expected by Select2
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data, except to indicate that infinite
                // scrolling can be used
                params.page = params.page || 1;

                return {
                    results: data,
                    pagination: {
                        more: (params.page * 30) < data.total_count
                    }
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) {
            return markup;
        },
        //data: $.getJSON(url, {besId: selectedBes, keyword: ''}, function (data) {
        //    return {results: data};
        //}),
        placeholder: "--Please search--",
        minimumInputLength: 2,
        allowClear: true
    });
}

我解决此问题的方法是设置minimumInputLength = 0,编写一个自定义ajax传输器,以检查搜索项的长度,当其等于0时,我使用固定的默认值调用成功回调,否则,如果大于0则执行ajax调用并返回响应。

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

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