将json结果恢复为select2时出现问题.我的json没有返回具有“文本”字段的结果,因此需要格式化结果,以便select2接受“名称”.
如果json中的文本字段设置为“文本”,则此代码有效,但是在这种情况下,我无法更改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',
但我仍然收到“文本未定义”.
谢谢你的帮助.
解决方法:
请注意,select2始终为{id,text}对,因此您需要同时指定两个
results: function (data, page) {
var newData = [];
_.each(data, function (item) {
newData.push({
id: item.Id //id part present in data
, text: item.displayString //string to be displayed
});
});
return { results: newData };
}
},
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。