Skip to content

Instantly share code, notes, and snippets.

@pdrosos
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pdrosos/4ecc385daac4bcdaed39 to your computer and use it in GitHub Desktop.
Save pdrosos/4ecc385daac4bcdaed39 to your computer and use it in GitHub Desktop.
Bootstrap tagsinput with typeahead - setup and fix https://github.com/TimSchlechter/bootstrap-tagsinput/issues/200
var $keywordsInput = $('.image-keywords');
$keywordsInput.tagsinput({
confirmKeys: [13, 59, 188] // 13 - Enter 59 - ; 188 - ,
});
// create a bloodhound suggestion engine
var imageKeywords = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/get-keywords-suggestions/%text',
wildcard: '%text'
}
});
// initialize the bloodhound suggestion engine
imageKeywords.initialize();
// Adding custom typeahead support using http://twitter.github.io/typeahead.js
$keywordsInput.tagsinput('input').typeahead({
minLength: 2
}, {
name: 'image-keywords',
displayKey: 'value',
source: imageKeywords.ttAdapter()
}).bind('typeahead:selected', $.proxy(function (obj, datum) {
this.tagsinput('add', datum.value);
// we need to clean the typeahead value here
this.tagsinput('input').typeahead('val', '');
}, $keywordsInput));
$keywordsInput.on('itemAdded', function(event) {
// event.item: contains the item
// we need to clean the typeahead value here
$keywordsInput.tagsinput('input').typeahead('val', '');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment