【问题标题】:Select2 - how to display returned json dataSelect2 - 如何显示返回的 json 数据
【发布时间】:2014-06-12 09:01:30
【问题描述】:

这是从我的页面返回的 json 字符串的样子:

{"osoby":["jeden","dwa","trzy","cztery","piec"]}

Firebug 声称一切正常,但 select2 插件中没有显示任何数据。

有谁知道可能是什么问题?它应该返回列名还是什么?

select2 调用:

$("#complogi").select2({
    multiple: true,
    minimumInputLength: 3,
    ajax: {
        url: "cdop",
        dataType: 'json',
        type: 'POST',

        data: function (term, page) {
            return {
                q: term,
                action: 'getEventsComplogs'
            };
        },
        results: function (data) {
            return {results: data};
        }
    }
});

【问题讨论】:

  • 试试return {results: data.osoby};
  • @Saqueib, {results: data.osoby}; 在 firebog 中返回错误 typeError: a is undefinied select2.min.js
  • console.log(data) 看看返回的json是什么结构
  • @Saqueib {"osoby":["jeden","dwa","trzy","cztery","piec"]}
  • 添加这两行formatResult: function (item) { return item; },formatSelection: function (item) { return item; }修复问题

标签: jquery jquery-select2


【解决方案1】:

您的返回数据格式不正确,无法在 select2 中使用,您需要以下格式的数据

{id: 1, text: 'jeden'}

如果你能从后端修改它会很棒

格式化结果

formatResult: function (item) { return item; },
formatSelection: function (item) { return item; }

【讨论】:

    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多