Implement cascading comboboxes in jqxGrid.
A live JavaScript/jQuery example of jqxGrid, the jQWidgets Grid component, with its complete source code below. It is one of 139 jqxGrid demos for jQuery.
selectionmode property – Sets or gets the selection mode.autoheight property – Sets or gets the height of the grid to be equal to the summary height of the grid rows.width property – Sets or gets the Grid's width.columns property – Sets the Grid columns.editable property – The editable property enables or disables the Grid editing feature.source property – Sets and gets the source.getcellvalue method – Gets the value of a cell.setcellvalue method – Sets a new value to a cell.To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxGrid shows the setup step by step.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxbuttons.js, jqxscrollbar.js, jqxmenu.js, jqxgrid.js, jqxgrid.selection.js, jqxgrid.pager.js, jqxlistbox.js, jqxgrid.edit.js, jqxdropdownlist.js, jqxcombobox.js, jqxpanel.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).
$(document).ready(function () {
var data = '[{ "Country": "Belgium", "City": "Brussels"}, {"Country": "France", "City": "Paris"}, {"Country": "USA", "City": "Washington"}]';
// prepare the data
var source =
{
datatype: "json",
datafields: [
{ name: 'Country', type: 'string' },
{ name: 'City', type: 'string' }
],
localdata: data
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#grid").jqxGrid(
{
width: 300,
source: dataAdapter,
selectionmode: 'singlecell',
editable: true,
autoheight: true,
columns: [
{
text: 'Country', datafield: 'Country', width: 150, columntype: 'combobox',
cellvaluechanging: function (row, datafield, columntype, oldvalue, newvalue) {
if (newvalue != oldvalue) {
$("#grid").jqxGrid('setcellvalue', row, "City", "Select a city...");
};
}
},
{
text: 'City', datafield: 'City', width: 150, columntype: 'combobox',
initeditor: function (row, cellvalue, editor, celltext, cellwidth, cellheight) {
var country = $('#grid').jqxGrid('getcellvalue', row, "Country");
var city = editor.val();
var cities = new Array();
switch (country) {
case "Belgium":
cities = ["Bruges", "Brussels", "Ghent"];
break;
case "France":
cities = ["Bordeaux", "Lille", "Paris"];
break;
case "USA":
cities = ["Los Angeles", "Minneapolis", "Washington"];
break;
};
editor.jqxComboBox({ autoDropDownHeight: true, source: cities });
if (city != "Select a city...") {
var index = cities.indexOf(city);
editor.jqxComboBox('selectIndex', index);
}
}
}
]
});
});
<div id="grid">
</div>