Created
May 11, 2013 00:26
-
-
Save zirosas/5558406 to your computer and use it in GitHub Desktop.
bootstrap typeahead multiple selection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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