zTree動態載入子節點
阿新 • • 發佈:2018-12-31
我這個案例裡不是採用zTree預設的async進行非同步載入,而是在onExpand事件裡用ajax請求資料,
然後再動態的新增子節點,程式碼如下:
var setting = { data: { keep: { parent: true, } }, callback: { onExpand: expandNode, beforeExpand: beforeExpand, }, }; //展開節點 var parentNode = null; function expandNode(e, b, node) { if (node.AppID != null) { parentNode = node; $.ibo.crossOrgin({ url: "url", funcName: "function", data: "data", success: function (res) { if (res.ResFlag == $.ResFlag.Success) { if (res.ResObj && res.ResObj.List && res.ResObj.List.length > 0) { var newNodes = []; $.each(res.ResObj.List, function (i, n) { newNodes.push({ name: n.Decription, icon: "../../img/icon.png" }); }); var treeObj = $.fn.zTree.getZTreeObj("treeDiv"); treeObj.addNodes(parentNode, newNodes); } } } }); } }; function beforeExpand(e, node) { var treeObj = $.fn.zTree.getZTreeObj("treeDiv"); parentNode = node; treeObj.removeChildNodes(parentNode); };