This is a migrated thread and some comments may be shown as answers.

Deleting from ListBox does not work as expected

2 Answers 191 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Traqn
Top achievements
Rank 1
Traqn asked on 20 Mar 2019, 10:14 AM

Hello,
I am trying implement controls combo between TreeView and ListBox.
The available actions are Add - from TreeView to ListBox and Remove - only remove from the ListBox.
I am using simple buttons from both actions.

The problems after call ListBox.remove(..) are two:
1) the collection listBox.dataItems() remains the same - i.e. the item is deleted only from UI
2) the underlying select HTML controls remains the same - i.e. the MVC/Kenod validators validates the page even the ListBox seems empty

Here is my remove button handler:

function removeFromListBox() {
    var listBox = $("#myListBox").data("kendoListBox");
    var selected = listBox.select(); // this is an array
    listBox.remove(listBox.select()); // Does not deletes the actual data item
    console.log(listBox.dataItems()); // listBox.dataItems() is not reduced
}

2 Answers, 1 is accepted

Sort by
0
Traqn
Top achievements
Rank 1
answered on 20 Mar 2019, 10:55 AM

I confirmed that the remove problem is only for dynamically added items from the TreeView.

Here is the Add Method:

function addToListBoxFromTreeView() {
    var tv = $("#myTreeView").data("kendoTreeView");
    var listBox = $("#myListBox").data("kendoListBox");
 
    var selectedTreeItem = tv.dataItem(tv.select());
    var mega = { Id: selectedTreeItem.id, Label: selectedTreeItem.text };
 
    var existingSelected = listBox.dataItems().filter(function (item) { return item.Id == selectedTreeItem.id; });
 
    if (existingSelected.length === 0) {
        var item = listBox.add(mega);
    }
}
0
Alex Hajigeorgieva
Telerik team
answered on 25 Mar 2019, 09:39 AM
Hi, Traqan,

I used our Kendo UI ListBox official API demo to test if the remove() and add() methods behave in the described manner to no avail. Here is a simplified version which allows you to add and remove items and a couple of buttons that alert the result of the dataItems() method as well as the select options. Please check it out and let me know in case I have missed anything:

https://dojo.telerik.com/@bubblemaster/OPAZanef

Look forward to hearing back from you.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
ListBox
Asked by
Traqn
Top achievements
Rank 1
Answers by
Traqn
Top achievements
Rank 1
Alex Hajigeorgieva
Telerik team
Share this question
or