首页 > 其他分享 >js树形组件zTree简单使用

js树形组件zTree简单使用

时间:2022-11-19 10:00:53浏览次数:49  
标签:treeNode name pId js zTree 树形 true id

<!DOCTYPE html>
<html>
  <head>
    <title>ZTREE DEMO</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="./plugins/zTree_v3-master/css/zTreeStyle/zTreeStyle.css" type="text/css" />
    <link rel="stylesheet" href="./plugins/bootstrap-5.1.3-dist/css/bootstrap.min.css" type="text/css" />
    <script type="text/javascript" src="./plugins/zTree_v3-master/js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="./plugins/zTree_v3-master/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="./plugins/zTree_v3-master/js/jquery.ztree.excheck.js"></script>
    <script type="text/javascript" src="./plugins/bootstrap-5.1.3-dist/js/bootstrap.bundle.js"></script>
    <style>
      .ztree * {
        font-size: 1rem;
      }
    </style>
    <script type="text/javascript">
      const nodeTypes = [
        { id: "root", pId: "", name: "root" },
        { id: "comp1", pId: "root", name: "IM" },
        { id: "comp1-1", pId: "comp1", name: "DD" },
        { id: "comp1-1-1", pId: "comp1-1", name: "CY" },
        { id: "comp1-2", pId: "comp1", name: "CY" },
        { id: "comp1-3", pId: "comp1", name: "WG" },
        { id: "comp2", pId: "root", name: "CX" },
        { id: "comp3", pId: "root", name: "DD" },
        { id: "comp3-1", pId: "comp3", name: "CY" },
        { id: "comp4", pId: "root", name: "CY" },
        { id: "comp5", pId: "root", name: "WG" },
      ];

      var setting1 = {
        view: {
          selectedMulti: false,
          showIcon: false,
          nameIsHTML: true,
          dblClickExpand: dblClickExpand,
        },
        edit: {
          enable: true,
          showRemoveBtn: false,
          showRenameBtn: false,
        },
        data: {
          simpleData: {
            enable: true,
          },
        },
        callback: {
          onClick: onClick1,
          beforeRemove: beforeRemove1,
        },
      };

      const zTreeId = {
        id: 2,
      };
      var zNodes1 = [
        { id: 1, pId: 0, name: "root", open: true, ddesc: "root", dtype: "root", dfiled1: "", dfiled2: "" },
        { id: 2, pId: 1, name: "IM", open: true, ddesc: "IM", dtype: "comp1", dfiled1: "", dfiled2: "" },
      ];

      function dblClickExpand(treeId, treeNode) {
        return treeNode.level > 0;
      }

      function onClick1(e, treeId, treeNode) {
        console.log("clicked");
        $("#editId").val(treeNode.id);
        $("#dtype").val(treeNode.ddesc);
        $("#dfiled1").val(treeNode.dfiled1);
        $("#dfiled2").val(treeNode.dfiled2);
        $("#editForm").show();

        const options = getSubNodes(treeNode.dtype);
        if (options.length > 0) {
          let html = '<option value="">----</option>';
          options.forEach((item) => {
            html += '<option value="' + item.id + '">' + item.name + "</option>";
          });
          $("#subType").html(html);
          $("#addPartId").val(treeNode.id);
          $("#addPartForm").show();
        } else {
          $("#addPartForm").hide();
        }
        if (treeNode.id != 1) {
          $("#deletePart").show();
        } else {
          $("#deletePart").hide();
        }
      }

      function beforeRemove1(treeId, treeNode) {
        console.log("remove");
        return false;
      }

      function getSubNodes(id) {
        const nodes = [];
        nodeTypes.forEach((item) => {
          if (item.pId == id) {
            nodes.push(item);
          }
        });
        return nodes;
      }

      function editNode() {
        const nodeId = $("#editId").val();
        var zTree = $.fn.zTree.getZTreeObj("treeDemo1");
        var node = zTree.getNodeByParam("id", nodeId);
        node.dtype = $("#dtype").val();
        node.dfiled1 = $("#dfiled1").val();
        node.dfiled2 = $("#dfiled2").val();
        node.name = node.ddesc + '<span style="color:blue">' + node.dfiled1 + node.dfiled2 + "</span>";
        zTree.updateNode(node);
        // $("#editForm").hide();
      }

      function addPartNode() {
        const nodeId = $("#addPartId").val();
        var zTree = $.fn.zTree.getZTreeObj("treeDemo1");
        var node = zTree.getNodeByParam("id", nodeId);
        const nodeName = $("#subType option:selected").html();
        const nodeType = $("#subType").val();
        const isParent = getSubNodes(nodeType).length > 0 ? true : false;
        const newNode = { id: zTreeId.id + 1, pId: nodeId, isParent: isParent, name: nodeName, open: true, ddesc: nodeName, dtype: nodeType, dfiled1: "", dfiled2: "" };
        zTree.addNodes(node, newNode);
        zTreeId.id += 1;
        console.log(nodeName, nodeType, zTree.getNodes());
        $("#subType").val("");
        $("#num").val("");
        // $("#addPartForm").hide();
      }

      function deletePartNode() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo1"),
          nodes = zTree.getSelectedNodes(),
          treeNode = nodes[0];
        if (nodes.length == 0) {
          alert("请先选择一个节点");
          return;
        }
        if (!confirm("确认删除 节点 -- " + treeNode.name + "及其子节点 吗?")) {
          return false;
        }
        zTree.removeChildNodes(treeNode);
        zTree.removeNode(treeNode);
      }

      $(document).ready(function () {
        $.fn.zTree.init($("#treeDemo1"), setting1, zNodes1);
      });
    </script>

    <script>
      //--------------------DOWM-------------------->
      var setting2 = {
        view: {
          selectedMulti: false,
          showIcon: false,
          nameIsHTML: true,
        },
        check: {
          enable: true,
        },
        data: {
          simpleData: {
            enable: true,
          },
        },
        callback: {
          beforeCheck: beforeCheck2,
          onClick: onClick2,
        },
      };

      var zNodes2 = [
        { id: 1, pId: 0, name: "root", open: true },
        { id: 2, pId: 1, name: "IM", open: true },
        { id: 3, pId: 2, name: "DD", open: true },
        { id: 4, pId: 3, name: "CY", open: true },
        { id: 5, pId: 1, name: "DD", open: true },
        { id: 6, pId: 5, name: "CY", open: true },
        { id: 7, pId: 1, name: "CX", open: true },
        { id: 8, pId: 1, name: "CY", open: true },
        { id: 9, pId: 1, name: "WG", open: true },
      ];

      function beforeCheck2(treeId, treeNode) {
        return treeNode.doCheck !== false;
      }

      function onClick2(e, treeId, treeNode) {
        $(".type-form").hide();
        $("#" + treeNode.name + "Form").show();
      }

      $(document).ready(function () {
        $.fn.zTree.init($("#treeDemo2"), setting2, zNodes2);
      });
    </script>
  </head>

  <body class="bg-light">
    <div class="container-fluid">
      <div class="row mt-4">
        <div class="col-3">
          <div class="card" style="height: 400px">
            <div class="card-header">树状图</div>
            <div class="card-body">
              <ul id="treeDemo1" class="ztree"></ul>
            </div>
          </div>
        </div>
        <div class="col-3">
          <div class="card mb-3" style="height: 275px; display: none" id="editForm">
            <div class="card-header">编辑</div>
            <div class="card-body">
              <div class="form">
                <form>
                  <input type="hidden" name="did" id="editId" value="" />
                  <div class="input-group mb-3">
                    <span class="input-group-text">name</span>
                    <input type="text" class="form-control" id="dtype" name="dtype" disabled />
                  </div>
                  <div class="input-group mb-3">
                    <span class="input-group-text" id="basic-addon2">字段1</span>
                    <input type="text" class="form-control" id="dfiled1" name="dfiled1" />
                  </div>
                  <div class="input-group mb-3">
                    <span class="input-group-text">字段2</span>
                    <input type="text" class="form-control" id="dfiled2" name="dfiled2" />
                  </div>
                  <div class="input-group">
                    <button type="button" class="btn btn-primary" onclick="editNode()">确定</button>
                  </div>
                </form>
              </div>
            </div>
          </div>
          <div class="card" style="height: 220px; display: none" id="addPartForm">
            <div class="card-header">新增子节点</div>
            <div class="card-body">
              <div class="form">
                <form>
                  <input type="hidden" name="did" id="addPartId" value="" />
                  <div class="input-group mb-3">
                    <span class="input-group-text">name</span>
                    <select class="form-control" name="subType" id="subType">
                      <option value="">----</option>
                    </select>
                  </div>
                  <div class="input-group mb-3">
                    <span class="input-group-text" id="basic-addon2">数量</span>
                    <input type="text" class="form-control" id="num" name="num" />
                  </div>
                  <div class="input-group">
                    <button type="button" class="btn btn-primary" onclick="addPartNode()">新增</button>
                  </div>
                </form>
              </div>
            </div>
          </div>
          <div class="card p-3 mt-3" id="deletePart" style="display: none">
            <button class="btn btn-primary" onclick="deletePartNode()">删除此节点</button>
          </div>
        </div>
        <div class="col-3">
          <div class="card" style="height: 400px">
            <div class="card-header">组成树状图</div>
            <div class="card-body">
              <ul id="treeDemo2" class="ztree"></ul>
            </div>
          </div>
        </div>
        <div class="col-3">
          <div class="card mb-3" style="height: 400px">
            <div class="card-header">编辑</div>
            <div class="card-body">
              <div id="rootForm" class="type-form" style="display: none">
                <nav>
                  <div class="nav nav-pills" role="tablist">
                    <a class="nav-link active" id="nav1-1-tab" data-bs-toggle="tab" data-bs-target="#nav1-1" role="tab" aria-controls="nav1-1" aria-selected="true">BZ</a>
                    <a class="nav-link" id="nav1-2-tab" data-bs-toggle="tab" data-bs-target="#nav1-2" role="tab" aria-controls="nav1-2" aria-selected="false">ZC</a>
                  </div>
                </nav>
                <div class="tab-content">
                  <div class="tab-pane fade show active" id="nav1-1" role="tabpanel" aria-labelledby="nav1-1-tab">ROOT BZ</div>
                  <div class="tab-pane fade" id="nav1-2" role="tabpanel" aria-labelledby="nav1-2-tab">ROOT ZC</div>
                </div>
              </div>
              <div id="IMForm" class="type-form" style="display: none">
                <nav>
                  <div class="nav nav-pills" role="tablist">
                    <a class="nav-link active" id="nav2-1-tab" data-bs-toggle="tab" data-bs-target="#nav2-1" role="tab" aria-controls="nav2-1" aria-selected="true">BZ</a>
                    <a class="nav-link" id="nav2-2-tab" data-bs-toggle="tab" data-bs-target="#nav2-2" role="tab" aria-controls="nav2-2" aria-selected="false">ZC</a>
                  </div>
                </nav>
                <div class="tab-content">
                  <div class="tab-pane fade show active" id="nav2-1" role="tabpanel" aria-labelledby="nav2-1-tab">IM BZ</div>
                  <div class="tab-pane fade" id="nav2-2" role="tabpanel" aria-labelledby="nav2-2-tab">IM ZC</div>
                </div>
              </div>
              <div id="CXForm" class="type-form" style="display: none">
                <nav>
                  <div class="nav nav-pills" role="tablist">
                    <a class="nav-link active" id="nav3-1-tab" data-bs-toggle="tab" data-bs-target="#nav3-1" role="tab" aria-controls="nav3-1" aria-selected="true">BZ</a>
                    <a class="nav-link" id="nav3-2-tab" data-bs-toggle="tab" data-bs-target="#nav3-2" role="tab" aria-controls="nav3-2" aria-selected="false">ZC</a>
                  </div>
                </nav>
                <div class="tab-content">
                  <div class="tab-pane fade show active" id="nav3-1" role="tabpanel" aria-labelledby="nav3-1-tab">CX BZ</div>
                  <div class="tab-pane fade" id="nav3-2" role="tabpanel" aria-labelledby="nav3-2-tab">CX ZC</div>
                </div>
              </div>
              <div id="DDForm" class="type-form" style="display: none">
                <nav>
                  <div class="nav nav-pills" role="tablist">
                    <a class="nav-link active" id="nav4-1-tab" data-bs-toggle="tab" data-bs-target="#nav4-1" role="tab" aria-controls="nav4-1" aria-selected="true">BZ</a>
                    <a class="nav-link" id="nav4-2-tab" data-bs-toggle="tab" data-bs-target="#nav4-2" role="tab" aria-controls="nav4-2" aria-selected="false">ZC</a>
                  </div>
                </nav>
                <div class="tab-content">
                  <div class="tab-pane fade show active" id="nav4-1" role="tabpanel" aria-labelledby="nav4-1-tab">DD BZ</div>
                  <div class="tab-pane fade" id="nav4-2" role="tabpanel" aria-labelledby="nav4-2-tab">DD ZC</div>
                </div>
              </div>
              <div id="CYForm" class="type-form" style="display: none">
                <nav>
                  <div class="nav nav-pills" role="tablist">
                    <a class="nav-link active" id="nav5-1-tab" data-bs-toggle="tab" data-bs-target="#nav5-1" role="tab" aria-controls="nav5-1" aria-selected="true">BZ</a>
                    <a class="nav-link" id="nav5-2-tab" data-bs-toggle="tab" data-bs-target="#nav5-2" role="tab" aria-controls="nav5-2" aria-selected="false">ZC</a>
                  </div>
                </nav>
                <div class="tab-content">
                  <div class="tab-pane fade show active" id="nav5-1" role="tabpanel" aria-labelledby="nav5-1-tab">CY BZ</div>
                  <div class="tab-pane fade" id="nav5-2" role="tabpanel" aria-labelledby="nav5-2-tab">CY ZC</div>
                </div>
              </div>
              <div id="WGForm" class="type-form" style="display: none">
                <nav>
                  <div class="nav nav-pills" role="tablist">
                    <a class="nav-link active" id="nav6-1-tab" data-bs-toggle="tab" data-bs-target="#nav6-1" role="tab" aria-controls="nav6-1" aria-selected="true">BZ</a>
                    <a class="nav-link" id="nav6-2-tab" data-bs-toggle="tab" data-bs-target="#nav6-2" role="tab" aria-controls="nav6-2" aria-selected="false">ZC</a>
                  </div>
                </nav>
                <div class="tab-content">
                  <div class="tab-pane fade show active" id="nav6-1" role="tabpanel" aria-labelledby="nav6-1-tab">WG BZ</div>
                  <div class="tab-pane fade" id="nav6-2" role="tabpanel" aria-labelledby="nav6-2-tab">WG ZC</div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>
 

 

标签:treeNode,name,pId,js,zTree,树形,true,id
From: https://www.cnblogs.com/caroline2016/p/16905512.html

相关文章

  • layer和sweetalert2消息提醒js插件简单使用
    <!DOCTYPEhtml><htmllang="en"backgound=""><head><metacharset="UTF-8"/><metahttp-equiv="X-UA-Compatible"content="IE=edge"/><metaname......
  • Vue中使用Mock,devSever中before方法弃用>webpack新版本出现的vue.config.js配置问题:op
    话不多说直接上代码:1、mock相关配置(mock/index.js),这里仅使用 setupMiddlewares其余旧版级过渡版本方法见官网1//引入mock2constMock=require('mockjs');......
  • Vue.js -- 条件渲染
    条件渲染v-if的用法代码演示:<head><title>vue条件渲染</title><!--使用CDN引入Vue--><scriptsrc="https://unpkg.com/vue@next"></script></head><b......
  • simpread-(132 条消息) three.js 加载 stl 文件并解析_攻城狮 plus 的博客 - CSDN 博
    threejs加载STL文件效果:加载stl格式的文件需要设置材质material;stl、obj都是静态模型,不可以包含动画,fbx除了包含几何、材质信息,可以存储骨骼动画等数据。代码:......
  • js Blob 与 ArrayBuffer
    1/*2*Blob3*4*Blob(BinaryLargeObject)表示二进制类型的大对象,在数据库管理系统中,将二进制数据存储为一个单一个体的集合5*Blob通常是影像、......
  • JS 选项卡
    思路:需要标题和页面内容两个部分,几个标题就对应几个页面,页面设置定位使其重叠,在美化一下css样式即可当点击某个标题时,显示出与标题相关的内容,同时标题样式发生变化,然后关......
  • nodejs.01
    fs模块导入fs模块,来操作文件constfs=require('fs');调用fs.readFile()方法读取文件参数1:读取文件存放路径参数2:读取文件时候采用的编码格式,一般默认utf-8参......
  • C#利用js脚本实现配置的文本表达式计算
    usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingMSScriptControl;namespaceMyQuery.CSharpScript{  ///......
  • 二级联动 js脚本
    //二级联动说明:联动的select的值必须匹配allinfo为二级所有值的数组格式为value|text  //贾世义functionselectChange(obj,changeId,allinfo){   varselV......
  • Nodejs报错记录
    1.digitalenveloperoutines::unsupportedD:\workspace\vuedemo>npmrundev...error:0308010C:digitalenveloperoutines::unsupportedatnewHash(node:int......