How to change the datasource without triggering the change event

1 Answer 198 Views
DropDownTree
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
Lee asked on 18 Jul 2023, 08:57 PM | edited on 20 Jul 2023, 01:07 PM

I have a kendo dropdown tree and when I set the datasource after initialization via .setDataSource([...]) it triggers the change event. How do I prevent this from happening? I am setting the datasource with a local javascript array of objects. I saw in another forum post to set the value to an empty array. I tried that but it still triggered the on change event. I'm doing this because I have 2 dropdowntrees (one in a flyout panel and one in the header) that I need to keep in sync.

Here is a dojo demonstrating my issue
https://dojo.telerik.com/iKoBOVIk/30

1 Answer, 1 is accepted

Sort by
1
Nikolay
Telerik team
answered on 21 Jul 2023, 12:55 PM

Hello Lee,

You can unbind the change event before calling the setDataSource() method and bind it back after it finishes:

    $("#test").click(function(){
      dropdowntree.value(""); //clear the dropdown list
      dropdowntree.unbind("change");
      dropdowntree.setDataSource(test_dataSource2);
      dropdowntree.bind("change");
      dropdowntree.value(4);
     })

Dojo demo: https://dojo.telerik.com/iKoBOVIk/33

Regards,
Nikolay
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
commented on 21 Jul 2023, 02:40 PM | edited

This partially works but if you look in the console you'll notice it still logs "2 changed". The weird thing is that 2 is never actually touched. 

I feel like this is a bug in Kendo UI.

Nikolay
Telerik team
commented on 26 Jul 2023, 08:17 AM

Hi Lee,

You should not pass to the setDataSource() a reference of an existing data source of another ListView but rather create a new data source:

let test_dataSource2 = new kendo.data.HierarchicalDataSource({
        data: test_dataSource
      })

      // clear dropdown values on button click
      $("#test").click(function(){
        dropdowntree.value(""); //clear the dropdown list
        dropdowntree.setDataSource(test_dataSource2);
        dropdowntree.value(4);
        dropdowntree.trigger("change");
      })

Dojo demo: https://dojo.telerik.com/uSEnOdOp

Regards,

Nikolay

Tags
DropDownTree
Asked by
Lee
Top achievements
Rank 2
Bronze
Bronze
Bronze
Answers by
Nikolay
Telerik team
Share this question
or