Select2 4.0.0 不支持标记,无法通过 ajax 调用获取标记项
Select2 4.0.0 not supporting tagging, not able to get tags items through ajax call
我无法使用 select2 4.0 版开发一个选项来建议存储在数据库中的标签项目,通过 ajax 调用在写下所需标签名称的几个字母(2,3 个字母)后调用。
因为版本的改变initSelection
以前版本的方法已经不存在了。它抛出一个错误 "No select2/compat/initSelection".
这是我在以前版本中使用的代码:
$('.addTags').select2({
placeholder: "Problem Tags",
allowClear: true,
minimumInputLength: 2,
tags: true,
createSearchChoice: function (term, data) {
if ($(data).filter(function () {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return {
id: term,
text: term
};
}
},
initSelection: function (element, callback) {
var tags = element.val().split(",");
var data = [];
for (var i = 0; i < tags.length; i++) {
data.push({ id: tags[i], text: tags[i] });
}
callback(data);
},
multiple: true,
ajax: {
type: 'GET',
url: "/Problem/GetTags",
dataType: 'json',
data: function (term, page) {
return {
term: term,
page_limit: 15
};
},
results: function (data, page) {
return { results: data.tags };
}
}
});
使用 Select2 4.0.0 版本您必须将 createSearchChoice
更改为 createTag
:
createTag: function (params) {
var term = $.trim(params.term);
if (term === '') {
return null;
}
return {
id: term,
text: term,
newTag: true // add additional parameters
}
}
那么一般不需要initSelection
(参见https://select2.org/upgrading/migrating-from-35#removed-the-requirement-of-initselection)。
在 ajax 选项中将 results
处理程序更改为 processResults
:
processResults: function (data, page) {
return { results: data.tags };
},
例如,这是一个 jsfiddle:https://jsfiddle.net/beaver71/bhodkpav/
我无法使用 select2 4.0 版开发一个选项来建议存储在数据库中的标签项目,通过 ajax 调用在写下所需标签名称的几个字母(2,3 个字母)后调用。
因为版本的改变initSelection
以前版本的方法已经不存在了。它抛出一个错误 "No select2/compat/initSelection".
这是我在以前版本中使用的代码:
$('.addTags').select2({
placeholder: "Problem Tags",
allowClear: true,
minimumInputLength: 2,
tags: true,
createSearchChoice: function (term, data) {
if ($(data).filter(function () {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return {
id: term,
text: term
};
}
},
initSelection: function (element, callback) {
var tags = element.val().split(",");
var data = [];
for (var i = 0; i < tags.length; i++) {
data.push({ id: tags[i], text: tags[i] });
}
callback(data);
},
multiple: true,
ajax: {
type: 'GET',
url: "/Problem/GetTags",
dataType: 'json',
data: function (term, page) {
return {
term: term,
page_limit: 15
};
},
results: function (data, page) {
return { results: data.tags };
}
}
});
使用 Select2 4.0.0 版本您必须将 createSearchChoice
更改为 createTag
:
createTag: function (params) {
var term = $.trim(params.term);
if (term === '') {
return null;
}
return {
id: term,
text: term,
newTag: true // add additional parameters
}
}
那么一般不需要initSelection
(参见https://select2.org/upgrading/migrating-from-35#removed-the-requirement-of-initselection)。
在 ajax 选项中将 results
处理程序更改为 processResults
:
processResults: function (data, page) {
return { results: data.tags };
},
例如,这是一个 jsfiddle:https://jsfiddle.net/beaver71/bhodkpav/