weixin_33737774 2016-09-07 18:12 采纳率: 0%
浏览 63

Select2预载数据

Currently select2 only loads data once user starts typing. I want to preload the drop down with some default values, so once the user clicks on the drop down there are already some values to select from.. I've looked at the documentation but I couldn't find anything. Does anybody know if there is a workaround or something I could do to achieve this.

Thank you

Edit:

$('#search-paint').select2({
                placeholder: "Search By:Manufacturer, Color Name, Color Code",
                ajax: {
                    url: "/item/searchPaint",
                    dataType: "json",
                    delay: 250,
                    data: function (params) {
                        return {
                            q: params.term,
                            SprayType: $('#spray-type').val(),
                            page: params.page
                        };
                    },
                    processResults: function (data, params) {
                        params.page = params.page || 1;
                        return {
                            results: data.items.data,
                            pagination: {
                                more: (data.items.current_page * data.items.per_page) < data.items.total
                            }
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (markup) {
                    return markup;
                },
                minimumInputLength: 1,
                templateResult: formatRepo,
                templateSelection: formatRepoSelection
            });

I've tried preloading data using the data setting, but it only loads the first object and is calls the select method. I've also tried initSelection but later I discovered it has a different purpose from what I'm trying to achieve.

  • 写回答

1条回答 默认 最新

  • weixin_33743880 2016-09-08 11:23
    关注

    You can do something like this

    var data = [];
    $('#search-paint').select2({
        data:data,
        escapeMarkup: function (markup) {
           return markup;
        },
        minimumInputLength: 1,
        templateResult: formatRepo,
        templateSelection: formatRepoSelection
    });
    $.getJSON('/item/searchPaint',function(response){
        data=response;
        $('#search-paint').select2('refresh');
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。
  • ¥15 已知隐函数其中一个变量τ的具体值,求另一个变量
  • ¥15 r语言Hurst指数
  • ¥15 RT-Thread Studio编译问题