Configure the Cascading ComboBox
19 Jun 20247 minutes to read
The cascading ComboBox is a series of ComboBox, where the value of one ComboBox depends upon another’s value. This can be configured by using the change event of the parent ComboBox. Within that change event handler, data has to be loaded to the child ComboBox based on the selected value of the parent ComboBox.
The following example shows the cascade behavior of country, state, and city ComboBox. Here, the dataBind method is used to reflect the property changes immediately to the ComboBox.
<div class="col-lg-12 control-section">
<div class='control-wrapper'>
<div class="padding-top">
@Html.EJS().ComboBox("country").Placeholder("Select a country").PopupHeight("auto").Change("countrychange").DataSource((IEnumerable<Object>
)ViewBag.country).Fields(new Syncfusion.EJ2.DropDowns.ComboBoxFieldSettings { Text = "CountryName", Value = "CountryId" }).Render()
@*//change="countrychange"*@
</div>
<div class="padding-top">
@Html.EJS().ComboBox("state").Placeholder("Select a state").Enabled(false).Change("statechange").PopupHeight("auto").DataSource((IEnumerable<object>)ViewBag.state).Fields(new Syncfusion.EJ2.DropDowns.ComboBoxFieldSettings { Text = "StateName", Value = "StateId" }).Render()
@*// change="statechange"*@
</div>
<div class="padding-top">
@Html.EJS().ComboBox("city").Placeholder("Select a city").Enabled(false).PopupHeight("auto").DataSource((IEnumerable<object>
)ViewBag.cities).Fields(new Syncfusion.EJ2.DropDowns.ComboBoxFieldSettings { Text = "CityName", Value = "CityId" }).Render()
</div>
</div>
</div>
<script>
function countrychange() {
var state = document.getElementById('state').ej2_instances[0];
var city = document.getElementById('city').ej2_instances[0];
var country = document.getElementById('country').ej2_instances[0];
// disable the state DropDownList
state.enabled = true;
// frame the query based on selected value in country DropDownList.
var tempQuery = new ej.data.Query().where('CountryId', 'equal', country.value);
// set the framed query based on selected value in country DropDownList.
state.query = tempQuery;
// set null value to state DropDownList text property
state.text = null;
// bind the property changes to state DropDownList
state.dataBind();
// set null value to city DropDownList text property
city.text = null;
// disable the city DropDownList
city.enabled = false;
// bind the property changes to City DropDownList
city.dataBind();
}
function statechange() {
var city = document.getElementById('city').ej2_instances[0];
var state = document.getElementById('state').ej2_instances[0];
city.enabled = true;
// Query the data source based on state DropDownList selected value
var tempQuery1 = new ej.data.Query().where('StateId', 'equal', state.value);
// set the framed query based on selected value in city DropDownList.
city.query = tempQuery1;
//clear the existing selection
city.text = null;
// bind the property change to city DropDownList
city.dataBind();
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
public class State
{
public string StateName { get; set; }
public string CountryId { get; set; }
public string StateId { get; set; }
public List<State> StateList()
{
List<State> state = new List<State>();
state.Add(new State() { StateName = "New York", CountryId = "1", StateId = "101" });
state.Add(new State() { StateName = "Queensland", CountryId = "2", StateId = "104" });
state.Add(new State() { StateName = "Tasmania ", CountryId = "2", StateId = "105" });
state.Add(new State() { StateName = "Victoria", CountryId = "2", StateId = "106" });
state.Add(new State() { StateName = "Virginia ", CountryId = "1", StateId = "102" });
state.Add(new State() { StateName = "Washington", CountryId = "1", StateId = "103" });
return state;
}
}
}
NOTE