kendo treeview checkbox - parent node check - chan

2019-08-14 14:10发布

问题:

I have a 2-level Kendo UI Treeview with parent and children nodes. When I check a parent node, all children nodes (including parent) are automatically checked.

Now I want to send an event with current tree selection state. The problem is when I try to do it in dataSource.change method because it fires on every checkbox selection separately (1 parent node + 3 children nodes = 4 change events). This is what the code looks like:

treeView.data("kendoTreeView").dataSource.bind("change", function() {
    alert('event fired!');
});

I've created a working example here

Is there another event to attach to to have only one event fired after all checkboxes are updated? Or maybe there is a way to group all those 'change' events and fire just one instead?

回答1:

I've ended with this solution:

tree.data("kendoTreeView").dataSource.bind("change", function() {
        treeView = tree.data("kendoTreeView");
        checkedNodes = [];
    checkedNodeIds(treeView.dataSource.view(), checkedNodes);
    if (!timeoutSet) {
        setTimeout(fireCheckboxChangeOnce, 50);//fires only one event when parent checkbox checks all the children
        timeoutSet = true;
    }
});

Only the first event creates a delayed call to function (using setTimeout()). All the remaining "change" events only modify the checkedNodes parameter.

fireCheckboxChangeOnce function also clears the timeoutSet flag.