【发布时间】:2015-05-22 16:39:57
【问题描述】:
我正在尝试将 select2 ajax 调用与模板一起使用。我可以很好地使用 ajax,但它没有使用我的模板函数。
ajax 数据为:
[
{"name":"First thing","otherData":"asdfg"},
{"name":"Second thing","otherData":"qqerr"},
{"name":"third thing","otherData":"yerty"},
{"name":"fourth thing","otherData":"hgjfgh"},
{"name":"fifth thing","otherData":"fhgkk"}
]
select2 代码(我从here 拿了很多东西)是:
FundSearch = {
create: function (selector, theURL) {
$(selector).select2({
ajax: {
url: theURL,
dataType: 'json',
delay: 250,
data: function (params) {
console.log(params.term);
return {
q: params.term, // search term
page: params.page
};
},
results: function (data, page) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data
};
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
templateResult: formatData,
templateSelection: formatDataSelection
});
function formatData (data) {
if (data.loading) return data.name;
markup = "<h1>" + data.name + "</h1>" + "<p>" + data.otherData + "</p>";
return markup;
}
function formatDataSelection (data) {
return data.name;
}
}
};
我得到的错误是Uncaught TypeError: Cannot read property 'toUpperCase' of undefined 在select2.js 的第356 行,版本:3.5.2。
在我看来,select2 没有使用我的 templateSelection 和 templateResults 函数。
【问题讨论】: