Skip to content

Instantly share code, notes, and snippets.

@masdimdev
Forked from ajaxray/ select2-cascade.js
Created April 9, 2018 07:48
Show Gist options
  • Select an option

  • Save masdimdev/73630c662dfb818f6a80e402a23b115b to your computer and use it in GitHub Desktop.

Select an option

Save masdimdev/73630c662dfb818f6a80e402a23b115b to your computer and use it in GitHub Desktop.
Making Select2 (4.x) list boxes cascading / dependent. Options of a select2 list box will be loaded/refreshed by ajax based on selection of another select2 list box.

Select2 Cascade (for v4.x)

Loadeding/refreshing options of a select2 list box using ajax based on selection of another select2 list box.

How to use

Create a new instance of Select2Cascade by passing the following 4 things -

  • Parent select2 listbox
  • Child select2 listbox
  • URL to load child items on selection changed
  • Optional data object to be sent with request. The selected value of parent selects listbox will be set in this data object as parent_id

The response json should be flat object of value:label pairs. e,g,

{
  20150415: "Chittagong Zila",
  20190901: "Comilla Zila",
  20221601: "Cox's Bazar Zila",
  20301401: "Feni Zila"
}

Examples

When #parentList is changed, call to /path/to/geocode?type=district&parent_id=SELECTED-PARENT and set the options of #childList from response.

new Select2Cascade($('#parentList'), $('#childList'), 'path/to/geocode', {type:"district", parent_id: ''});

If you want to do something with the response data or selectboxes, you can set (any number of) callbacks to be executed after the child listbox refreshed -

var cascadLoading = new Select2Cascade($('#parentList'), $('#childList'), 'path/to/geocode', {type:"district", parent_id: ''});
cascadLoading
    .then( function(parent, child, items) {
        // Open the child listbox immediately
        child.select2('open');
    }).then(function(parent, child, items) {
        // Dump response data
        console.log(items);
    });
var Select2Cascade = ( function(window, $) {
function Select2Cascade(parent, child, url, requestData) {
var afterActions = [];
var requestData = requestData || {};
// Register functions to be called after cascading data loading done
this.then = function(callback) {
afterActions.push(callback);
return this;
};
parent.select2().on("change", function (e) {
requestData.parent_id = $(this).val();
child.prop("disabled", true);
var _this = this;
$.getJSON(url, requestData, function(items) {
var newOptions = '<option value="">-- Select --</option>';
for(var id in items) {
newOptions += '<option value="'+ id +'">'+ items[id] +'</option>';
}
child.select2('destroy').html(newOptions).prop("disabled", false)
.select2({ width: 'resolve', placeholder: "-- Select --" });
afterActions.forEach(function (callback) {
callback(parent, child, items);
});
});
});
}
return Select2Cascade;
})( window, $);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment