Select2不使用我的templateResults或templateSelection选项 [英] Select2 not using my templateResults or templateSelection options
问题描述
我正在尝试将select2 ajax调用与模板一起使用.我可以正常使用ajax,但是它没有使用我的模板功能.
I am trying to use the select2 ajax call with templates. I am getting the ajax in just fine but it is not using my template functions.
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代码(我从此处花费了很多时间)是:
the select2 code (which i took a lot from here ) is:
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.
The error I am getting is Uncaught TypeError: Cannot read property 'toUpperCase' of undefined
on line 356 of select2.js
for Version: 3.5.2.
在我看来,select2没有使用我的templateSelection和templateResults函数.
It seems to me that select2 is not using my templateSelection and templateResults function.
推荐答案
您正在查看4.0.0文档,但正在使用3.5.2. 您仍然可以访问3.5.2文档.
You are looking at the 4.0.0 documentation but are using 3.5.2. You can still access the 3.5.2 documentation.
具体地说,templateSelection
和templateResult
选项仅在4.0.0中存在.在3.5.2中将它们分别称为formatSelection
和formatResult
.
Specifically, the templateSelection
and templateResult
options only exist in 4.0.0. They were called formatSelection
and formatResult
in 3.5.2.
这篇关于Select2不使用我的templateResults或templateSelection选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!