Home >Web Front-end >JS Tutorial >ztree implements dynamic spanning tree on the left and content details function example sharing on the right
This article mainly shares with you ztree's function of dynamically generating trees on the left and content details on the right. zTree uses the core code of JQuery to implement a set of Tree plug-ins that can complete most common functions. It is compatible with IE, FireFox, Chrome and other browsers. Multiple Tree instances can be generated simultaneously in one page. Supports JSON data. Supports one-time static generation and Ajax asynchronous loading. Supports multiple event responses and feedback. Supports tree node movement, editing, and deletion. Supports arbitrary skin/personalized icon changes. (relying on css).
Page prototype:
Functional requirements: Click on the child node in the tree on the left to send a request to the background and display the requested information on the right
Front-end code implementation in the form:
Introduce css document:
<link rel="stylesheet" type="text/css" href="<c:url value=" rel="external nofollow" rel="external nofollow" rel="external nofollow" /js/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.css"/>" /> <link rel="stylesheet" type="text/css" href="<c:url value=" rel="external nofollow" rel="external nofollow" rel="external nofollow" /js/bower_components/ztree_v3/css/zTreeStyle/zTreeStyle.css"/>" /> <link rel="stylesheet" type="text/css" href="<c:url value=" rel="external nofollow" rel="external nofollow" rel="external nofollow" /css/global/ztree_custom.css"/>" />
Introduce js file:
<script type="text/javascript" src="<c:url value="/js/bower_components/ztree_v3/js/jquery.ztree.core-3.5.min.js"/>"></script> <script type="text/javascript" src="<c:url value="/js/bower_components/ztree_v3/js/jquery.ztree.exedit-3.5.min.js"/>"></script> <script type="text/javascript" src="<c:url value="/js/bower_components/ztree_v3/js/jquery.ztree.excheck-3.5.min.js"/>"></script> <script src="<c:url value="/js/system/organ.js"/>"></script>
jsp part: The HTML part is very simple, it is quite In the p
<ul id="organTree" class="ztree"style=" overflow :auto;"></ul>
js part of a blooming tree:
Set tree nodes
var setting = { check : { enable : false }, view : { selectedMulti : false, // addHoverDom: addHoverDom, // removeHoverDom: removeHoverDom, }, data : { key : { name : "name" }, simpleData : { enable : true, idKey : "id", pIdKey : "pId" } }, edit : { enable : true, removeTitle : "删除节点", showRemoveBtn : $("#pdelete").val() == "delete" ? setRemoveBtn : false, showRenameBtn : false }, callback : { // onRightClick : onRightClick, // 单击事件 onClick : zTreeOnClick, onNodeCreated : zTreeOnNodeCreated, beforeRemove : zTreeBeforeRemove, onRemove : zTreeOnRemove } };
Initialization, determine whether to expand the node:
var zTreeObj; function initTree() { $.get(basePath + "/system/organ/getOrganTreeList", function(data) { zTreeObj = $.fn.zTree.init($("#organTree"), setting, data.returnData.organTree); zTreeObj.expandAll(false); }); } // 给生成的节点添加class属性 // 控制节点是否显示删除图标 function setRemoveBtn(treeId, treeNode) { return treeNode.pId != null; } // 给生成的节点添加class属性 function zTreeOnNodeCreated(event, treeId, treeNode) { var str = treeNode.tId + "_span"; $("#" + str).addClass(treeNode.type); }
Click event , like a background request, requesting the information on the right
// 单击事件,向后台发起请求 function zTreeOnClick(event, treeId, treeNode) { if (treeNode.id == "1") { return; } $("#moreinform").show(); $("#baseinform").hide(); $(".po_phone_num_r").css("display", "none"); $(" .po_email_r").css("display", "none"); if (treeNode.type == "organ") { $("#organ").html("部门名称"); $("#Partman").show(); $("#Email").hide(); $("#sorgan").html("上级部门"); $("#partaddress").html("部门地址"); $("#partman").html("部门负责人"); $("#parttel").html("手机"); if (treeNode.id == "1") { $("#po").hide(); } else { $("#po").show(); } $.ajax({ url : basePath + "/system/organ/" + treeNode.id, type : "get", success : function(data) { var organ = data.returnData.organ; $("#organId").val(organ.organId); $("#sex").hide(); $("#name").val(organ.organName); $("#diz").val(organ.address); $("#tel").val(organ.phone); $("#manage").val(organ.manager); $("#parentOrgan").val(organ.parentId); } }); } else { $("#po").show(); $("#organ").html("姓名"); $("#sex").show(); $("#Email").show(); $("#Partman").hide(); $("#sorgan").html("所属部门"); $("#partaddress").html("职位"); $("#parttel").html("手机"); $.ajax({ url : basePath + "/system/organ/getStaff/" + treeNode.id, type : "get", success : function(data) { var staff = data.returnData.staff; $("#organId").val(staff.id); $("#name").val(staff.name); $("#diz").val(staff.position); $("#tel").val(staff.tel); $("#profession").val(staff.sex) $("#Email02").val(staff.email); $("#parentOrgan").val(staff.organId); } }); } }
Delete event:
##
// 删除节点事件 function zTreeOnRemove(event, treeId, treeNode) { if (treeNode.type == "organ") { $.ajax({ url : basePath + "/system/organ/" + treeNode.id, type : "DELETE", success : function(data) { $("#confirmDialog").modal("hide"); // 点击删除按钮,隐藏弹框 if (customGlobal.ajaxCallback(data)) { location.reload(); } } }); } else { $.ajax({ url : basePath + "/system/organ/deleteStaff/" + treeNode.id, type : "DELETE", success : function(data) { $("#confirmDialog").modal("hide"); // 点击删除按钮,隐藏弹框 if (customGlobal.ajaxCallback(data)) { initTree(); } } }); } }Related recommendations:
Example Explain jQuery's use of zTree plug-in to implement drag-and-drop function
Detailed examples of zTree jQuery tree plug-in usage
Detailed examples of jQuery's use of ztree to implement tree shapes sheet
The above is the detailed content of ztree implements dynamic spanning tree on the left and content details function example sharing on the right. For more information, please follow other related articles on the PHP Chinese website!