使用Select2仅加载一次远程数据 [英] Loading remote data only once with Select2

查看:112
本文介绍了使用Select2仅加载一次远程数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,我只想加载一次远程数据. 我曾考虑过使用独立的ajax调用加载数据并在控件上本地"设置数据,但想知道是否还有更多内置"方式...

As the title suggests I would like to load remote data once only. I thought about loading a data with independent ajax call and set it "locally" at the control but wonder if there is more "built in" way to do so...

推荐答案

可以在此处找到解决方案:

a solution can be found here:

https://github.com/ivaynberg/select2/issues/110

$("#selIUT").select2({
    cacheDataSource: [],
    placeholder: "Please enter the name",
    query: function(query) {
        self = this;
        var key = query.term;
        var cachedData = self.cacheDataSource[key];

        if(cachedData) {
            query.callback({results: cachedData.result});
            return;
        } else {
            $.ajax({
              url: '/ajax/suggest/',
              data: { q : query.term },
              dataType: 'json',
              type: 'GET',
              success: function(data) {
                self.cacheDataSource[key] = data;
                query.callback({results: data.result});
              }
            })
        }
    },
    width: '250px',
    formatResult: formatResult, 
    formatSelection: formatSelection, 
    dropdownCssClass: "bigdrop", 
    escapeMarkup: function (m) { return m; } 
}); 

我可能误解了您的问题.如果您希望一次加载所有数据,然后使用Select2,则没有内置功能可以做到这一点.

I might have misinterpreted your question. if you wish to load all data once, then use that is Select2, there is no built in functionality to do that.

您的建议是进行单个查询,然后使用Select2中存储的数据进行操作.

Your suggestion to do a single query, and then use that stored data in Select2 would be the way to go.

这篇关于使用Select2仅加载一次远程数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆