Skip to content

Commit a739a4c

Browse files
kb(grid): improve cascading dropdowns KB to handle initial data load (#999)
* kb(grid): improve cascading dropdowns KB to handle initial data load * kb(grid): hint for other LOD for cascading dropdowns in a grid
1 parent 15a5fc0 commit a739a4c

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

knowledge-base/grid-cascade-dropdowns-for-fields.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ res_type: kb
2525
I want to have the options for one of the fields in the grid to depend on the choice of another. In other words, to cascade one dropdown from the other in the grid columns.
2626

2727
## Solution
28-
There are two approaches you can take:
28+
There are three approaches you can take:
2929

3030
* For full freedom, implement a custom edit form (here are examples for <a href="https://demos.telerik.com/blazor-ui/grid/editing-custom-form" target="_blank">inline</a>, and <a href="https://github.com/telerik/blazor-ui/tree/master/grid/custom-popup-form" target="_blank">popup</a>)
3131

32-
* Implement the general approach for [cascading dropdowns]({%slug dropdown-kb-cascading%}) in the [editor templates]() of those fields. a key thing is to create new data collections, and to use the OnChange event.
32+
* Implement the general approach for [cascading dropdowns]({%slug dropdown-kb-cascading%}) in the [editor templates]({%slug grid-templates-editor%}) of those fields. a key thing is to create new data collections, and to use the OnChange event. You may also want to handle the `OnEdit` event of the grid to provide initial data for the second column.
33+
34+
* Use load on demand for the dropdowns themselves (their `OnRead` event) so that when they initialize, they will fire the event, and you can load the data there. The component fires the event when needed and you can use the currently edited item you store in the view-model to provide more information to your service.
35+
3336

3437
>caption Example of cascading dropdowns in grid editor templates in popup edit mode (works for inline mode too)
3538
3639
````CSHTML
3740
@* Field 1 determines what you see in the cascaded field. The code comments offer some more details *@
3841
39-
<TelerikGrid Data=@GridData EditMode="@GridEditMode.Popup" Pageable="true" Height="300px" OnUpdate="@UpdateHandler">
42+
<TelerikGrid Data=@GridData EditMode="@GridEditMode.Popup" Pageable="true" Height="300px"
43+
OnUpdate="@UpdateHandler" OnEdit="@EditHandler">
4044
<GridColumns>
4145
<GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="ID" />
4246
<GridColumn Field=@nameof(SampleData.Name) Title="Name" />
@@ -77,21 +81,39 @@ There are two approaches you can take:
7781
public List<SampleData> GridData { get; set; }
7882
public List<string> SecondFieldData { get; set; }
7983
84+
// These two event handlers implement loading data on demand to cascade the value of the second
85+
// column through the value of the first. The OnEdit handler is to pre-load data for the second column
86+
// when there is already data for the first one, but none for the second
87+
// Modify the logic and use appropriate services in an actual application, this is just a basic example
88+
8089
async Task CascadeSecondList(object newVal)
8190
{
8291
await Task.Delay(300); // simulate service/data call, remove in real app
8392
8493
// "load" data - in this case just show some relation to the first field
8594
List<string> theNewData = Enumerable.Range(1, 5).Select(x => $"option {x} for {CurrentlyEditedEmployee.Field1}").ToList();
8695
// add the current value so that it is found in the data source and the drodown does not revert to the default value of the type
87-
if (!string.IsNullOrEmpty(CurrentlyEditedEmployee.Field2))
96+
Console.WriteLine(theNewData.IndexOf(CurrentlyEditedEmployee.Field2));
97+
if (!string.IsNullOrEmpty(CurrentlyEditedEmployee.Field2) &&
98+
theNewData.IndexOf(CurrentlyEditedEmployee.Field2) == -1 // don't add the existing item a second time
99+
)
88100
{
89101
theNewData.Add(CurrentlyEditedEmployee.Field2);
90102
}
91103
// update the data source, a key thing is to uise a new reference (new collection) and not to Add/Remove items from an existing one
92104
SecondFieldData = theNewData;
93105
}
94106
107+
async Task EditHandler(GridCommandEventArgs args)
108+
{
109+
if(CurrentlyEditedEmployee is null)
110+
{
111+
CurrentlyEditedEmployee = args.Item as SampleData;
112+
}
113+
await CascadeSecondList(null);
114+
}
115+
116+
95117
// sample data to get the thing running
96118
97119
public void UpdateHandler(GridCommandEventArgs args)

0 commit comments

Comments
 (0)