40zTree - 保持展开单一路径

简介: 40zTree - 保持展开单一路径

效果图:

代码:

<!DOCTYPE html>
<HTML>
<HEAD>
  <TITLE> ZTREE DEMO - single path</TITLE>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" href="../../../css/demo.css" type="text/css">
  <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
  <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
  <script type="text/javascript" src="../../../js/jquery.ztree.core.js"></script>
  <!--  <script type="text/javascript" src="../../../js/jquery.ztree.excheck.js"></script>
    <script type="text/javascript" src="../../../js/jquery.ztree.exedit.js"></script>-->
  <SCRIPT type="text/javascript">
    <!--
    var setting = {
      view: {
        dblClickExpand: false,
        showLine: false
      },
      data: {
        simpleData: {
          enable: true
        }
      },
      callback: {
        beforeExpand: beforeExpand,
        onExpand: onExpand,
        onClick: onClick
      }
    };
    function createNodes(maxNodesNumInLevel, maxLevel, curLevel, curPId) {
      if (maxNodesNumInLevel<5) {
        maxNodesNumInLevel = 5;
      }
      var nodes = [], num = 0;
      while(num<3) {
        num = parseInt(Math.random()*1024)%maxNodesNumInLevel+1;
      }
      for (var i=0; i<num; i++) {
        var id = curPId ? curPId + "-" + i : "" + i, isParent = (parseInt(Math.random()*9999)%3!=0),
        node = {id: id, pId : curPId, name : "N" + id};
        nodes.push(node);
        if (isParent && curLevel<maxLevel) {
          nodes = nodes.concat(createNodes(maxNodesNumInLevel, maxLevel, curLevel+1, id));
        }
      }
            return nodes;
    }
    var zNodes =createNodes(5, 5, 0);
    var curExpandNode = null;
    function beforeExpand(treeId, treeNode) {
      var pNode = curExpandNode ? curExpandNode.getParentNode():null;
      var treeNodeP = treeNode.parentTId ? treeNode.getParentNode():null;
      var zTree = $.fn.zTree.getZTreeObj("treeDemo");
      for(var i=0, l=!treeNodeP ? 0:treeNodeP.children.length; i<l; i++ ) {
        if (treeNode !== treeNodeP.children[i]) {
          zTree.expandNode(treeNodeP.children[i], false);
        }
      }
      while (pNode) {
        if (pNode === treeNode) {
          break;
        }
        pNode = pNode.getParentNode();
      }
      if (!pNode) {
        singlePath(treeNode);
      }
    }
    function singlePath(newNode) {
      if (newNode === curExpandNode) return;
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
                    rootNodes, tmpRoot, tmpTId, i, j, n;
            if (!curExpandNode) {
                tmpRoot = newNode;
                while (tmpRoot) {
                    tmpTId = tmpRoot.tId;
                    tmpRoot = tmpRoot.getParentNode();
                }
                rootNodes = zTree.getNodes();
                for (i=0, j=rootNodes.length; i<j; i++) {
                    n = rootNodes[i];
                    if (n.tId != tmpTId) {
                        zTree.expandNode(n, false);
                    }
                }
            } else if (curExpandNode && curExpandNode.open) {
        if (newNode.parentTId === curExpandNode.parentTId) {
          zTree.expandNode(curExpandNode, false);
        } else {
          var newParents = [];
          while (newNode) {
            newNode = newNode.getParentNode();
            if (newNode === curExpandNode) {
              newParents = null;
              break;
            } else if (newNode) {
              newParents.push(newNode);
            }
          }
          if (newParents!=null) {
            var oldNode = curExpandNode;
            var oldParents = [];
            while (oldNode) {
              oldNode = oldNode.getParentNode();
              if (oldNode) {
                oldParents.push(oldNode);
              }
            }
            if (newParents.length>0) {
              zTree.expandNode(oldParents[Math.abs(oldParents.length-newParents.length)-1], false);
            } else {
              zTree.expandNode(oldParents[oldParents.length-1], false);
            }
          }
        }
      }
      curExpandNode = newNode;
    }
    function onExpand(event, treeId, treeNode) {
      curExpandNode = treeNode;
    }
    function onClick(e,treeId, treeNode) {
      var zTree = $.fn.zTree.getZTreeObj("treeDemo");
      zTree.expandNode(treeNode, null, null, null, true);
    }
    $(document).ready(function(){
      $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    });
    //-->
  </SCRIPT>
  <style type="text/css">
.ztree li button.switch {visibility:hidden; width:1px;}
.ztree li button.switch.roots_docu {visibility:visible; width:16px;}
.ztree li button.switch.center_docu {visibility:visible; width:16px;}
.ztree li button.switch.bottom_docu {visibility:visible; width:16px;}
  </style>
 </HEAD>
<BODY>
<h1>保持展开单一路径</h1>
<h6>[ 文件路径: super/singlepath.html ]</h6>
<div class="content_wrap">
  <div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>
  </div>
  <div class="right">
    <ul class="info">
      <li class="title"><h2>实现方法说明</h2>
        <ul class="list">
        <li>此 Demo 是在 "单击展开/折叠节点" 基础上改造而来,树节点保持始终只展开一条路径。</li>
        <li class="highlight_red">利用 setting.callback.beforeExpand / onExpand 事件回调函数实现展开规则</li>
        </ul>
      </li>
    </ul>
  </div>
</div>
</BODY>
</HTML>
目录
相关文章
53zTree - 异步加载模式下全部展开
53zTree - 异步加载模式下全部展开
58 1
|
JavaScript 前端开发 Java
ztree实现左边动态生成树,右边为具体信息功能
ztree实现左边动态生成树,右边为具体信息功能
68 0
Ant Design TreeSelect树形选择器格式化数据以及禁用父节点
Ant Design TreeSelect树形选择器格式化数据以及禁用父节点
1450 0
Ant Design TreeSelect树形选择器格式化数据以及禁用父节点
13zTree - 展开 / 折叠父节点控制
13zTree - 展开 / 折叠父节点控制
61 0
13zTree - 展开 / 折叠父节点控制
|
JavaScript 前端开发 Java
69jqGrid -层级加载时展开所有行
69jqGrid -层级加载时展开所有行
39 0
60EasyUI 树形菜单- 树形网格惰性加载节点
60EasyUI 树形菜单- 树形网格惰性加载节点
50 0
|
JavaScript
js点击收缩展开文本(不能用废弃)
js点击收缩展开文本(不能用废弃)
|
JavaScript 前端开发
前端祖传三件套JavaScript的DOM之节点层次、类型、属性
随着互联网的不断发展,前端技术也在不断地更新迭代。但是,无论怎么更新变化,JavaScript、DOM、CSS 三件套的重要性始终不可替代。其中,JavaScript 的作用尤为关键。本文将聚焦于 JavaScript 中的 DOM,讨论节点层次、类型和属性等概念。
155 0
|
JavaScript
js基础笔记学习214元素得属性和节点1方式1
js基础笔记学习214元素得属性和节点1方式1
86 0
js基础笔记学习214元素得属性和节点1方式1
|
JavaScript
树形组件(可动态添加属性、无限嵌套)及递归展示tree数据
树形组件(可动态添加属性、无限嵌套)及递归展示tree数据
树形组件(可动态添加属性、无限嵌套)及递归展示tree数据

热门文章

最新文章