Duplicated
Last Updated: 22 Feb 2021 10:10 by ADMIN
Vilmos
Created on: 18 Feb 2021 05:47
Category: DropDownList
Type: Bug Report
0
DropdownList sets the value to default after the bound data has been updated

I have two cascaded dropdownlists, in attached example car brands and selectable colors. The dependent dropdown has defaulttext option. After the parent dropdown has changed, in its event handler I reload the dependent data source and set a new valid value. If the data source does not contain the previous (!) value, the dropdownlist calls the valuechanged callback with default value. In next round when dependent dropdown holds the default value, after reloading datasource it works as expected. And parent changes again, if the updated datasource does not contain the previous dependent value the dropdownlist sets its value to default.

When I do not pass defaulttext parameter, the dependent dropdown selects the first element from the updated datasource. In my case it is not acceptable because I want to set a specific value (in this example the default color), not the first and not the default.

This feedback drives me to try setting value to default and wait for re-render the descendant dropdownlist before set the wanted value. 

Is there any way to avoid this behaviour of the dropdownlist?


<TelerikDropDownList TItem="Brand"
			 TValue="int"
			 ValueField=@nameof(Brand.Id)
			 TextField=@nameof(Brand.Name)
			 Data="@BrandList"
			 Value="@Data.BrandId"
			 ValueChanged=@OnBrandChanged />

<TelerikDropDownList TItem="Color"
			 TValue="int"
			 ValueField=@nameof(Brand.Id)
			 TextField=@nameof(Brand.Name)
			 Data="@FilteredColorList"
			 Value="@Data.ColorId"
			 ValueChanged="@OnColorChanged"
			 DefaultText="select color ..." />

async Task OnBrandChanged(int brandId) { Data.ColorId = default; await Task.Delay(5);

Data.BrandId = brandId; Data.ColorId = GetDefaultColor(brandId); FilteredColorList = GetFilteredColorList(brandId); }


Attached Files:
Duplicated
This item is a duplicate of an already existing item. You can find the original item here:
3 comments
ADMIN
Marin Bratanov
Posted on: 22 Feb 2021 10:10

Hi Vilmos,

Thank you for getting back to me.

Indeed, the ValueChanged event should not fire like that and we've reviewed this case too - the issue (and solution) are the same as here: https://feedback.telerik.com/blazor/1471160-selection-should-not-reset-value-on-data-change. Please Follow that page for status updates, I am marking this one as a duplicate to keep the portal tidy and reduce confusion in the future. Your sample was helpful and is one of the test cases for this scenario.

Regards,
Marin Bratanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Vilmos
Posted on: 18 Feb 2021 14:40

Hello Marin,

Thanks for quick response.

It would be perfect if I change the datasource of the second dropdown and set a value from the fresh dataset, the dropdown should not call the eventchanged callback with 0 or null value but shows the appropriate value (and its text) I have set. I'm looking for this behaviour.

In the example at a first run everything's fine. But when you change the brand to another item, it loads the available colors, set value to brand's default color (in example McLaren default color is Yellow, Id:4). But after this the descendant dropdown calls eventchanged callback with 0 (default) value and dropdown shows the text passed in defaulttext parameter.

In the feedback I mentioned in my bugreport it gave me a workaround: set a descendant dropdown value to default(TValue) (0 in this case), delay for a short while until dropdown re-renders. If you comment these two lines I hope you will experience the issue.


BR,

Vilmos

ADMIN
Marin Bratanov
Posted on: 18 Feb 2021 13:03

Hi Vilmos,

I will begin with what I perceive as the outstanding issue - the ValueChanged handler firing with the default value - what would you expect that the dropdown would do when its Data collection does not hold an item that matches the Value? I suppose you would expect the Value to remain the same so you can deal with the rest? If so, please click the Follow button on the original request for that: https://feedback.telerik.com/blazor/1471160-selection-should-not-reset-value-on-data-change.

Handling values that are out of the range of the available data is something that the application must handle, and the DefaultText could mask that to an extent, but it is still something that the app must take care of (more here). The component simply cannot know what it should do in this case, whatever it does is wrong in some cases, which is why we must leave it to the app.

I think what you are doing in the attached file is the correct approach and I cannot see issues in its behavior - when I change the car brand I see the default text as I would expect since there is no matching item in the datasource and the app code has taken care of setting that. I am testing with the current latest (2.21.1 at the time of writing) which has the fix for the other issue you linked as well.

Am I missing something? Is the behavior described in the first paragraph what you are looking for?

Regards,
Marin Bratanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.