我正在尝试将 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
在线 356 of select2.js
版本:3.5.2。在我看来 select2 没有使用我的 templateSelection 和 templateResults 函数。
最佳答案
您正在查看 4.0.0 文档,但使用的是 3.5.2。 You can still access the 3.5.2 documentation.
具体来说,templateSelection
和 templateResult
选项仅存在于 4.0.0 中。他们被称为 formatSelection
和 formatResult
在 3.5.2.
关于templates - Select2 不使用我的 templateResults 或 templateSelection 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29146743/