【发布时间】:2014-02-05 06:25:55
【问题描述】:
我在将 json 结果返回到 select2 时遇到问题。我的 json 不返回具有“文本”字段的结果,因此需要格式化结果以便 select2 接受“名称”。
如果 json 中的文本字段设置为“text”,则此代码有效,但在这种情况下,我无法更改 json 结果的格式(我无法控制的代码)。
$("#e1").select2({
formatNoMatches: function(term) {return term +" does not match any items." },
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
url: "localhost:1111/Items.json",
dataType: 'jsonp',
cache: true,
quietMillis: 200,
data: function (term, page) {
return {
q: term, // search term
p: page,
s: 15
};
},
results: function (data, page) { // parse the results into the format expected by Select2.
var numPages = Math.ceil(data.total / 15);
return {results: data.Data, numPages: numPages};
}
}
});
我查看了文档,发现了一些可以放入结果中的语句,例如
text: 'Name',
但我仍然收到“文本未定义”。
感谢您的帮助。
【问题讨论】:
标签: javascript jquery ajax json jquery-select2