Multiple value input in Typeahead
I am need the ability to enter multiple values in Typeahead. This is needed to enter tags, for example. Values can be separated by delimiter symbols or with help of custom tokenizer (optional).
Cool, but we don't have that. Sorry.
I've been using https://github.com/max-favilli/tagmanager for a tagging like feature with use of the typeahead plugin.. it works pretty good for my use case, so it might come in handy for you to
@Yohn, thank you!
jQueryUI multiple tag input sample remake:
var split = function (val) {
return val.split(/\s*[,;]\s*/);
};
var extractLast = function (term) {
return split(term).pop();
};
$(".tags-box").typeahead({
source: function (query, process) {
$.getJSON("/Tags/AutoCompletionList", { term: extractLast(query) }, process);
},
updater: function (item) {
var terms = split(this.query);
terms.pop();
terms.push(item);
terms.push("");
return terms.join(", ");
},
matcher: function (item) {
return extractLast(this.query).length > 0;
},
highlighter: function (item) {
var query = extractLast(this.query).replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>';
});
}
});
I have this (rather ugly) solution using a comma as delimiter. Works nice though!
Look for this.query in the typeahead section (around row 1900) in bootstrap.js.
Add something like:
if (this.multiple)
// element value > last word after comma > trimmed
this.query = this.$element.val().split(/,/).slice(-1)[0].replace(/^\s\s*/, '').replace(/\s\s*$/, '')
Now in your own JS:
// type ahead multiple support
$('.typeahead-multiple').typeahead({
multiple: true,
source: $this.data('source'),
updater: function(item) {
var val = '',
tags = $this.val().split(/,/);
tags.pop();
for (var i = 0; i < tags.length; i++) tags[i] = tags[i].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
if (tags.length > 0) val = tags.join(', ') + ', ' + item + ', ';
else val = item + ', ';
return val;
}
});