同じ問題に直面していたので、@ Ravindraのヒント(+1 BTW)を使用して、プラグインをリバースエンジニアリングし、tagSource関数が何を返すかを理解できるかどうかを確認しました。
tagSource関数はブール値を返します。availableTags配列のタグがオートコンプリートリストに表示されている場合は、Trueが返されます。タグが表示されないことを示すためにFalseが返されます。
これがデフォルトのtagSource関数で、indexOfを使用して、これまでに入力されたテキストがavailableTags配列のタグの先頭と一致するかどうかを判別します。
元のデフォルトの機能:
tagSource: function(search, showChoices) {
var filter = search.term.toLowerCase();
var choices = $.grep(this.options.availableTags, function(element) {
// Only match autocomplete options that begin with the search term.
// (Case insensitive.)
return (element.toLowerCase().indexOf(filter) === 0);
});
showChoices(this._subtractArray(choices, this.assignedTags()));
}
関数をコピーして.tagit関数に貼り付けたので、jQuerytagit初期化関数に渡されるパラメーターの1つとして含まれていました。次に、matchメソッドを使用するように変更しました。このメソッドは、パターンマッチングを使用して、パターンに一致する文字列の部分を返します。一致がnullを返す場合は、リストに表示しないでください。それ以外のものが返される場合は、リストにタグを表示します。
引数として渡された変更された関数:
tagSource: function(search, showChoices) {
var filter = search.term.toLowerCase();
var choices = $.grep(this.options.availableTags, function(element) {
// Only match autocomplete options that begin with the search term.
// (Case insensitive.)
//return (element.toLowerCase().indexOf(filter) === 0);
console.info(element.toLowerCase().match(filter) + " : " + element + " : " + filter);
return (element.toLowerCase().match(filter) !== null);
});
showChoices(this._subtractArray(choices, this.assignedTags()));
}
例:
$('#tagged').tagit({
onTagRemoved: function() {
alert("Removed tag");
},
availableTags: [ "one" , "two one" , "three" , "four" , "five" ],
// override function to modify autocomplete behavior
tagSource: function(search, showChoices) {
var filter = search.term.toLowerCase();
var choices = $.grep(this.options.availableTags, function(element) {
// Only match autocomplete options that begin with the search term.
// (Case insensitive.)
//return (element.toLowerCase().indexOf(filter) === 0);
console.info(element.toLowerCase().match(filter) + " : " + element + " : " + filter);
return (element.toLowerCase().match(filter) !== null);
});
showChoices(this._subtractArray(choices, this.assignedTags()));
}
});