Skip to content

Instantly share code, notes, and snippets.

@fmpwizard
Last active December 13, 2015 22:38
Show Gist options
  • Save fmpwizard/4985356 to your computer and use it in GitHub Desktop.
Save fmpwizard/4985356 to your computer and use it in GitHub Desktop.
(function(window, $) {
window.fmpwizard = {
views: {
DropDownMenus: function(){
var self = this;
self.initDropDown = function(selector, url){
if ($(selector).attr('class').contains('select2-offscreen')) {
//if we already init'ed this element, exit.
return;
}
$(selector).select2({
minimumInputLength:1,
"ajax": {
data:function (term, page) {
return { term:term, page:page };
},
dataType:"json",
quietMillis:100,
results: function (data, page) {
return {results: data.results};
},
"url": url
},
id: function(object) {
return object.text;
},
//Allow manually entered text in drop down.
createSearchChoice:function(term, data) {
if ( $(data).filter( function() {
return this.text.localeCompare(term)===0;
}).length===0) {
return {id:term, text:term};
}
},
});
}
}
}
};
})(this, jQuery);
<div class="control-group">
<label class="control-label" for="brand">Brand</label>
<div class="controls">
<input type="text" id="brand" placeholder="Brand" class="span4">
</div>
</div>
(function() {
$(document).ready(function() {
var dropDownMenus = new fmpwizard.views.DropDownMenus()
dropDownMenus.initDropDown("#brand", "/api/brands");
});
})();
@JasonCEC
Copy link

Can you explain how to include this?
Do we save the .js files to the same directory as select2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment