I'm using bootstrap-tokenfield with typeahead/bloodhound.
I can prevent the same token from being used twice in the tokenfield, but the same token still appears in the typeahead response. How can I exclude tokens that are already in the tokenfield?
var engine = new Bloodhound({
remote: {
url: API_URL + '?action=message_friends&q=%QUERY',
filter: function (response) {
return $.map(response.users, function (user) {
return {
value: user.user_id,
label: user.name
};
});
}
},
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace
);
engine.initialize();
$('#to-tags').tokenfield({
typeahead: [
{
hint: false
},
{
name: 'users',
displayKey: 'label',
source: engine.ttAdapter()
}
]
}).on('tokenfield:createtoken', function (event) {
var existingTokens = $(this).tokenfield('getTokens');
$.each(existingTokens, function(index, token) {
if (token.value === event.attrs.value) {
event.preventDefault();
}
});
});