Skip to content

Commit e086d85

Browse files
kb(dropdowns): cascading combo example
1 parent 4aa33e2 commit e086d85

File tree

1 file changed

+140
-2
lines changed

1 file changed

+140
-2
lines changed

knowledge-base/dropdown-cascading.md

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Use the `ValueChanged` event to update the model value and to filter the data fo
3030

3131
>caption Cascading DropDowns
3232
33-
````CSHTML
33+
````DropDownList
3434
@* Cascading componentsare disabled based on the selection of their parents. Events on parent components
3535
trigger data loading for child components so they show relevant result only.
3636
You can also see how to get the selected model from a dropdown component.
37-
This sample works with a ComboBox as well *@
37+
The same approach works for a ComboBox (just use nullable values), see the next example *@
3838
3939
<TelerikDropDownList Value="@CurrentOrder.CategoryId" Data="@Categories" DefaultText="Select Category"
4040
TextField="CategoryName" ValueField="CategoryId"
@@ -171,4 +171,142 @@ else if(!string.IsNullOrEmpty(orderStatusMessage))
171171
}
172172
}
173173
````
174+
````ComboBox
175+
@* The same approach works for the ComboBox, just make sure to use a nullable field so you can see the Placeholder *@
176+
177+
<TelerikComboBox Value="@CurrentOrder.CategoryId" Data="@Categories" Placeholder="Select Category"
178+
TextField="CategoryName" ValueField="CategoryId" Filterable="true"
179+
ValueChanged="@( (int? c) => CategorySelected(c) )">
180+
</TelerikComboBox>
181+
182+
<TelerikComboBox Value="@CurrentOrder.ProductId" Data="@CurrentProducts" Placeholder="Select Product" Filterable="true"
183+
TextField="ProductName" ValueField="ProductId" Enabled="@( CurrentOrder.CategoryId > 0 )"
184+
ValueChanged="@( (int? p) => ProductSelected(p) )">
185+
</TelerikComboBox>
186+
187+
@* The last dropdown can use two-way binding, it does not need to filter subsequent data *@
188+
<TelerikComboBox @bind-Value="@CurrentOrder.Quantity" Data="@Quantities" Placeholder="Select Quantity"
189+
Enabled="@( CurrentOrder.ProductId > 0 )">
190+
</TelerikComboBox>
191+
192+
<TelerikButton Enabled="@( CurrentOrder.Quantity > 0 )" OnClick="@SendOrder">Send Order</TelerikButton>
193+
194+
@if (CurrentOrder.CategoryId > 0)
195+
{
196+
<h5>Order Summary</h5>
197+
@CurrentOrder.CategoryName
198+
<br />
199+
@CurrentOrder.ProductName
200+
<br />
201+
@CurrentOrder.Quantity
202+
}
203+
else if (!string.IsNullOrEmpty(orderStatusMessage))
204+
{
205+
<div class="alert alert-success">@orderStatusMessage</div>
206+
}
207+
208+
@code{
209+
// data sources
210+
List<Category> Categories { get; set; }
211+
List<Product> AllProducts { get; set; }
212+
List<Product> CurrentProducts { get; set; }
213+
List<int> Quantities { get; set; }
214+
// model
215+
Order CurrentOrder { get; set; } = new Order();
216+
217+
string orderStatusMessage { get; set; } // UI related for the sample
218+
219+
// generate data we will be using in this example
220+
protected override void OnInitialized()
221+
{
222+
base.OnInitialized();
223+
224+
Categories = Enumerable.Range(1, 6).Select(x => new Category
225+
{
226+
CategoryId = x,
227+
CategoryName = $"Category {x}"
228+
}).ToList();
229+
230+
AllProducts = Enumerable.Range(1, 50).Select(x => new Product
231+
{
232+
ProductId = x,
233+
ProductName = $"Product {x}",
234+
CategoryId = (int)Math.Ceiling((double)x % 7)
235+
}).ToList();
236+
}
237+
238+
//ValueChanged handlers - implementation of cascading dropdowns
239+
void CategorySelected(int? category)
240+
{
241+
if (category == null) // the default value - the user selected the default item == deselected the current item
242+
{
243+
//reset the "form" / process
244+
CurrentOrder = new Order();
245+
return;
246+
}
247+
248+
// cascade the selection by filtering the data for the next dropdown
249+
CurrentProducts = AllProducts.Where(p => p.CategoryId == category).ToList();
250+
251+
// get the selected model from the data source
252+
Category SelectedCategory = Categories.Where(c => c.CategoryId == category).First();
253+
254+
// business logic
255+
CurrentOrder.CategoryId = SelectedCategory.CategoryId;
256+
CurrentOrder.CategoryName = SelectedCategory.CategoryName;
257+
}
258+
259+
void ProductSelected(int? product)
260+
{
261+
if (product == null) // the default value - the user selected the default item == deselected the current item
262+
{
263+
//reset the "form" / process
264+
CurrentOrder.ProductId = product;
265+
CurrentOrder.ProductName = string.Empty;
266+
CurrentOrder.Quantity = 0;
267+
return;
268+
}
269+
270+
Random rnd = new Random();
271+
Quantities = Enumerable.Range(1, new Random().Next(5, 10)).ToList();
272+
273+
Product SelectedProduct = AllProducts.Where(p => p.ProductId == product).First();
274+
275+
CurrentOrder.ProductId = SelectedProduct.ProductId;
276+
CurrentOrder.ProductName = SelectedProduct.ProductName;
277+
}
278+
279+
// sample notification of success and reseting of the process, data classes
280+
async void SendOrder()
281+
{
282+
CurrentOrder = new Order();
283+
orderStatusMessage = "Thank you for your order!";
284+
await Task.Delay(2000);
285+
orderStatusMessage = "";
286+
StateHasChanged();
287+
}
288+
289+
public class Category
290+
{
291+
public int CategoryId { get; set; }
292+
public string CategoryName { get; set; }
293+
}
294+
295+
public class Product
296+
{
297+
public int CategoryId { get; set; }
298+
public int ProductId { get; set; }
299+
public string ProductName { get; set; }
300+
}
301+
302+
public class Order
303+
{
304+
public int? CategoryId { get; set; }
305+
public string CategoryName { get; set; }
306+
public int? ProductId { get; set; }
307+
public string ProductName { get; set; }
308+
public int Quantity { get; set; }
309+
}
310+
}
311+
````
174312

0 commit comments

Comments
 (0)