由于在 kendo 树视图中添加了 child 项而更新其数据源时防止扩展 parent 节点

prevent expanding parent node when its datasource is updated due to addition of a child item inside kendo treeview

我有一个带 3 个根的 kendotreeview parent nodes.Example 如下所示。
当我将 child3 放入 New SubGroup 时,节点 "New Subgroup" 默认展开 就算之前崩了。我想防止这种可能性。如果之前扩展了New SubGroup,那么我想保持原样。问题是 expanddatabound 事件之前被调用,因此我被困在这里。

请帮忙。

parent1:

--New SubGroup  
--Child2  
--Child3 
--Child4   

parent2:

--Child4  
--Child5  

Code snippet:
 dataBound: function (e) {
            console.log("DataBound", e.node);
            var nodedataitem = $("#DimMeasTree").data("kendoTreeView").dataItem($(e.node));
            if (nodedataitem.FieldKey === "SubGroup" && ($(e.node).attr("aria-expanded")) === "true") {
                
                $("#DimMeasTree").data("kendoTreeView").collapse($(e.node));
           }
        }

我在初始化我的树视图后订阅了我自己的自定义扩展函数(即 subgroup_expand())。如下图所示:

<div id="treeview"></div>
<script>
function subgroup_expand(e) {
   if (typeof event === 'undefined') {
        //If browser is Firefox, the subgroup will expand and do not close automatically.
        // It is because Firefox does not support "event" attribute gloabally as in IE or in Google chrome.
    }
   else if (!!e.node && typeof(event) !== 'undefined' && event.type !== "click" && event.type !== "dblclick") {
        var nodedataitem = $("#treeview").data("kendoTreeView").dataItem($(e.node));
        if (nodedataitem.FieldKey === "SubGroup") {
            // e.preventDefault();
            setTimeout(function () {
                //Collapse the subgroup if it was not expanded by click or dblclick.
                $("#treeview").data("kendoTreeView").collapse($(e.node));
            }, 50);
        }
    }
}
$("#treeview").kendoTreeView({
  dataSource: modeldata
});
var treeview = $("#treeview").data("kendoTreeView");
treeview.bind("expand", subgroup_expand);
</script>