ztree展开节点信息存cookie,刷新自动显示之前展开信息
var callbackClick = function (event, treeId, treeNode) { var tree = $.fn.zTree.getZTreeObj(treeId); mainFrame.execute(tree, event, treeId, treeNode); tree.expandNode(treeNode, true, false, true); var cls; if (treeNode.orgLevel && treeNode.orgLevel != 2) { return; } if (treeNode.isParent == 0) { return; } }; //树形初始化与点击事件 var treeLoadFlags = [false, false, false, false, false]; var currTree; $(document).ready(function () { var trees = []; var type = [[${type}]]; var typeurl = type == 'mpg' ? '&type=mpg' : ''; for (var i = 0; i < plans.length; i++) { var tab = plans[i].tab; trees[trees.length] = new AsyncTree("/guidance/plan/asyncTree?planId=" + tab + typeurl, "#tree" + tab); trees[i].setting.callback = { onClick: callbackClick, onExpand: zTreeBeforeExpand, onCollapse: zTreeBeforeCollapse, //用于捕获异步加载出现异常错误的事件回调函数 onAsyncError: onAsyncError, //用于捕获异步加载正常结束的事件回调函数 onAsyncSuccess: onAsyncSuccess, onCheck: zTreeOnCheck }; trees[i].setting.check = { enable: [[${checked=='true'}]] }; $('#tabBtn' + tab).click(function () { var ti = this.value; if (!treeLoadFlags[ti]) { trees[ti].loadTree(); treeLoadFlags[ti] = true; } currTree = trees[ti]; }); } currTree = trees[0]; trees[0].loadTree();//加载第一颗树 }); /** 返回当前页签树选中的节点。 **/ function getCurrTreeCheckedNodes() { var nodes = currTree.tree.getCheckedNodes(true); return nodes; } function getCurrTreeSelectNodes() { var nodes = currTree.tree.getSelectedNodes(true); return nodes; } var zTreeBeforeExpand = function (event, treeId, treeNode) { // 树展开 var cookie = $.cookie(treeId + window.location); var z_tree = new Array(); if (cookie) { z_tree = JSON.parse(cookie); //console.log(z_tree); } if ($.inArray(treeNode.id, z_tree) < 0) { z_tree.push(treeNode.id); } $.cookie(treeId + window.location, JSON.stringify(z_tree)); }; var zTreeBeforeCollapse = function (event, treeId, treeNode) { var cookie = $.cookie(treeId + window.location); var z_tree = new Array(); if (cookie) { z_tree = JSON.parse(cookie); } //判断treeNode 在z_tree中的索引 var index = $.inArray(treeNode.id, z_tree); //删除 z_tree.splice(index, 1); for (var i = 0; i < treeNode.children.length; i++) { index = $.inArray(treeNode.children[i].id, z_tree); if (index > -1) { z_tree.splice(index, 1); } } $.cookie(treeId + window.location, JSON.stringify(z_tree)); }; function onAsyncSuccess(event, treeId, treeNode, msg) { var treeChecked = treeId+'checked'; var tree = $.fn.zTree.getZTreeObj(treeId); //alert(treeId + "haha"); var z_tree = new Array(); var z_tree_select = new Array(); var msgNode = new Array(); if (msg != null) { var json = JSON.parse(msg); for (var i = 0; i < json.length; i++) { msgNode.unshift(json[i].id); } } var cookie = $.cookie(treeId + window.location); var cookieSelect = $.cookie(treeChecked + window.location); if (cookie) { z_tree = JSON.parse(cookie); if(typeof (cookieSelect) !== "undefined"){ z_tree_select = JSON.parse(cookieSelect); } //2.首先判断根节点是否存在 if (msgNode.length > 0) { for (var j = 0; j < msgNode.length; j++) { var index = $.inArray(msgNode[j], z_tree); var indexchecked = $.inArray(msgNode[j], z_tree_select); //console.log(msgNode[j]) if (index != -1) { //3.把跟节点展开 //var node1 = tree.getNodeByTId(msgNode[j]); var node = tree.getNodeByParam("id", msgNode[j], null); tree.expandNode(node, true, false, true); } if (indexchecked != -1) { //3.把跟节点展开 //var node1 = tree.getNodeByTId(msgNode[j]); var nodechecked = tree.getNodeByParam("id", msgNode[j], null); tree.checkNode(nodechecked, true, true,false); //tree.expandNode(node, true, false, true); } } } } if (cookieSelect) { z_tree_select = JSON.parse(cookieSelect); //2.首先判断根节点是否存在 if (msgNode.length > 0) { for (var k = 0; k < msgNode.length; k++) { var index = $.inArray(msgNode[k], z_tree_select); //console.log(msgNode[j]) if (index != -1) { //3.把跟节点展开 //var node1 = tree.getNodeByTId(msgNode[j]); var node = tree.getNodeByParam("id", msgNode[k], null); tree.checkNode(node, true, true,false); //tree.expandNode(node, true, false, true); } } } } } function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest); } function zTreeOnCheck(event, treeId, treeNode) { var treeChecked = treeId+'checked'; var nodes = getCurrTreeCheckedNodes(); var z_tree_select = new Array(); for(var i = 0; i < nodes.length; i++){ var node = nodes[i]; z_tree_select.push(node.id); } $.cookie(treeChecked+ window.location, JSON.stringify(z_tree_select)); //alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked); }