[英]Select2 AJAX Tags
I have the following JSON, that is retrieved via /tags
: 我有以下JSON,可以通过
/tags
检索到:
[
{
"id": "CSS",
"text": "CSS"
},
{
"id": "HTML",
"text": "HTML"
},
{
"id": "JavaScript",
"text": "JavaScript"
},
{
"id": "jQuery",
"text": "jQuery"
},
{
"id": "MySQL",
"text": "MySQL"
},
{
"id": "PHP",
"text": "PHP"
}
]
I have an <input />
that is made to accept tags by using Select2: 我有一个
<input />
,它通过使用Select2来接受标签:
<input name="Tags" id="Tags" value="PHP,HTML,jQuery" />
And I have attached Select2 this way: 我以这种方式附加了Select2:
$("#Tags").select2({
tags: true,
tokenSeparators: [",", " "],
createSearchChoice: function(term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return {
id: term,
text: term
};
}
},
multiple: true,
ajax: {
url: '/tags',
dataType: "json",
data: function(term, page) {
return {
q: term
};
},
results: function(data, page) {
return {
results: data
};
}
}
});
Problems 问题
/tags
, but it doesn't load the tags. /tags
发出请求,但不会加载标签。 There are no errors in the console too. 控制台中也没有错误。 I am using Select2 3.5.2 from CDN.
我正在使用CDN中的Select2 3.5.2。 Where am I going wrong?
我要去哪里错了?
Well, I used this. 好吧,我用了这个。 Looks like a hack or work-around.
看起来像黑客或变通方法。
$("#Tags").select2({
tags: true,
multiple: true
});
$.getJSON("/tags", function (data) {
if (data.length > 0)
$("#Tags").select2({
tags: data,
multiple: true
});
});
Hope this helps for someone. 希望这对某人有帮助。 I will keep this open till I get better suggestions.
在我获得更好的建议之前,我将保持开放状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.