0
我正在嘗試使用Select2.js版本4.0創建標記選擇。我想要創建一個新的標籤,如果它沒有退出,並且我希望使用'data-html'
標籤格式化結果。Select2模板在標記時不會自動完成
問題是兩個元素(createTag
和templateResult
)獨立工作,但是當我將它們一起使用時,當用戶在搜索框中鍵入內容時,不再顯示自動填充選項。
我做錯了什麼,或者這是一個限制的Select2?
的application.js
$("#taglist").select2({
tokenSeparators: [',', ' '],
tags: true,
createTag: function (tag) {
// Case insensitive search for tag
tagExists = false;
$("#taglist option").each(function() {
if ($.trim(tag.term).toUpperCase() == $.trim($(this).text()).toUpperCase()) {
tagExists = true;
}
});
// If the tag does not exist create it
if (!tagExists) {
return {
id: tag.term,
text: tag.term + " (create)",
isNew: true
};
}
},
templateResult: function (choice) {
if (!choice.id) { return choice.text; }
var $choice = $(
'<span><a>' + data.element.getAttribute('data-html') + '</a></span>'
);
return $choice;
}
});