When you click Role1 in the top listbox it will display Person 1 and Person 2 once. Every subsequent click on Role1 or Role2 generates an additional duplicate in the listbox below it. Why is that? I tried all possible permutations of the parts in KH_ClearKendoListbox to get the listbox cleared before proceeding but that did not seem to help. The other reference to this issue on Stack Overflow suggested to use setDataSource but that did not help either. I am stuck and would love to know why this is happening in this scenario and how this might be solved so there is only one set of two items in the bottom listbox at all times.
<html>
<head runat="server">
<script type="text/javascript">
$(document).ready(function () {
var initDS;
var rolesDS = initDS = new kendo.data.DataSource({
data: [{ id1: 3, name1: "Role1" },
{ id1: 4, name1: "Role2" }]
});
$("#lstRoles").kendoListBox({
dataValueField: "id1",
dataTextField: "name1",
dataSource: rolesDS,
change: onChangeRoles,
}).data("kendoListBox");
function onChangeRoles(e) {
KH_ClearKendoListbox($("#lstIndividuals"));
initDS = new kendo.data.DataSource({
data: [{ id2: 3, name2: "Person 1" },
{ id2: 4, name2: "Person 2" }]
});
$("#lstIndividuals").kendoListBox({
dataValueField: "id2",
dataTextField: "name2",
}).data("kendoListBox").setDataSource(initDS);
}
function KH_ClearKendoListbox(lst) {
var listBox = lst.data("kendoListBox");
if (listBox === undefined) return;
var itemcount = listBox.dataSource._data.length;
for (var i = 0; i < itemcount; i++)
listBox.remove(listBox.items().first());
listBox.refresh();
listBox.clearSelection();
listBox.destroy();
}
});
</script>
</head>
<body>
<form id="form2" runat="server"
style="background-color: cornflowerblue">
<div class="container" style="padding:30px">
<select id="lstRoles" style="width: 265px"></select>
<br />
<select id="lstIndividuals" style="width:
265px; height: 233px">
</select>
</div>
</form>
</body>
</html>