Skip to content

Instantly share code, notes, and snippets.

@zirosas
Created May 11, 2013 00:26
Show Gist options
  • Save zirosas/5558406 to your computer and use it in GitHub Desktop.
Save zirosas/5558406 to your computer and use it in GitHub Desktop.
bootstrap typeahead multiple selection
function extractor(query) {
var result = /([^,]+)$/.exec(query);
if(result && result[1])
return result[1].trim();
return '';
}
var subjects = <%= raw autohash.to_json %>;
$('#search').typeahead({
source: subjects,
updater: function(item) {
return this.$element.val().replace(/[^,]*$/,'')+item+',';
},
matcher: function (item) {
var tquery = extractor(this.query);
if(!tquery) return false;
return ~item.toLowerCase().indexOf(tquery.toLowerCase())
},
highlighter: function (item) {
var query = extractor(this.query).replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>'
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment