Skip to content

Instantly share code, notes, and snippets.

@digerata
Created October 7, 2014 18:44
Show Gist options
  • Save digerata/469b14577d5c50c724f6 to your computer and use it in GitHub Desktop.
Save digerata/469b14577d5c50c724f6 to your computer and use it in GitHub Desktop.
$("input[data-role='tagsinput']").each(function() {
var placeholder = $(this).data('placeholder');
var saved = $(this).data('saved');
var autocomplete = jQuery(this).data('autocomplete');
jQuery(this).select2({
tags: true,
placeholder: placeholder,
minimumInputLength: 1,
ajax: {
quietMillis: 500,
url: "/users/" + autocomplete,
data: function(term, page) {
console.log("ajax.data: " + term);
return {
q: term
};
},
results: function(data, page) {
console.log("ajax.results: ");
console.log(data);
return {results: data};
}
},
initSelection: function(element, callback) {
console.log("initSelection: " + element);
saved && callback(saved);
},
createSearchChoice: function(term, data) {
if($(data).filter(function() {
console.log("filtering on " + term);
return this.name.localeCompare(term) === 0;
}).length === 0) {
console.log("length is zero:" + term);
return { id: term, name: term };
}
},
formatResult: function(item, page) {
console.log("format result: " + item);
if(item && item.name)
return item.name;
else
return "";
//return item.name;
},
formatSelection: function(item, page) {
console.log("format selection: " + item);
if(item && item.name)
return item.name;
else
return "";
// return item.name;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment