Kendo treeview making dups of item in listbox select even after being removed

1 Answer 137 Views
ListBox TreeView
David
Top achievements
Rank 1
David asked on 16 Jun 2021, 02:00 PM

Hey all I need a hand here. I've been at this for a few hours now and I just can't find how this code is bringing back the old value(s).

    const onCheck = (e) => {
        let listBox = $("#Sources").data("kendoListBox"),
            treeView = $("#availableSources").data("kendoTreeView"),
            selection = [],
            getSelection = (items) => {
                items.forEach(item => {
                    if (item.hasChildren) {
                        getSelection(item.items);
                    } else if (item.checked) {
                        selection.push(item);
                    }
                });
            };

        if (e.node.attributes[3].value == "false") {
            listBox.remove(listBox.items());
            $("#Sources option[value='" + listBox.dataSource._data[0].value + "']").remove();
            //selection.pop(listBox.dataSource._data[0]);
        } else {
            getSelection(treeView.dataSource.data());

            if (selection.length) {
                selection.forEach(item => {
                    listBox.add({
                        text: item.text,
                        value: item.id
                    });
                });
            }
        }
    }

As an example say I checked the item Github and it places it into the listbox just fine. It also adds the ID of the item to the hidden select component.

enter image description here

enter image description here

Now say I uncheck that item now:

enter image description here

enter image description here

Great! It removed the item from the listbox and also from the hidden select component. However though, when I chose another item, say GitHubIssues I am presented with not only that item in the listbox but the pervious item is placed into the select component.

enter image description here

enter image description here

It has the correct item in the listbox but I am unsure as to why its keeping the previous value(s)?

1 Answer, 1 is accepted

Sort by
1
Accepted
Veselin Tsvetanov
Telerik team
answered on 21 Jun 2021, 09:28 AM

Hello David,

Here you will find a small Dojo sample that implements the scenario in question:

https://dojo.telerik.com/EYAGaCUf/4

You will notice that I have slightly simplified the implementation so that the state of the ListBox is properly synced with the state of the TreeView after each check:

listBox.remove(listBox.items());
getSelection(treeView.dataSource.data());
if (selection.length) {
  selection.forEach(item => {
    listBox.add({
      text: item.name,
      value: item.name
    });
  });
}

Does the above implementation work for you? If not, can you modify the above Dojo so it replicates the exact issue observed at your end and send it back to me?

Regards,
Veselin Tsvetanov
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/.

David
Top achievements
Rank 1
commented on 21 Jun 2021, 05:30 PM

Yes thank you for the example, veselin.
Tags
ListBox TreeView
Asked by
David
Top achievements
Rank 1
Answers by
Veselin Tsvetanov
Telerik team
Share this question
or