您好,登錄后才能下訂單哦!
這篇文章主要介紹jQuery中zTree樹插件怎么用,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
靜態zTree開發流程
引入資源
<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>
html元素
<div> <ul id="treeDemo" class="ztree"></ul> </div>
JS方式一-簡單json數據格式
<SCRIPT type="text/javascript"> <!-- var setting = { data: { simpleData: { enable: true } } }; var zNodes =[ { id:1, pId:0, name:"父節點1 - 展開", open:true}, { id:11, pId:1, name:"父節點11 - 折疊"}, { id:111, pId:11, name:"葉子節點111"}, { id:112, pId:11, name:"葉子節點112"}, { id:113, pId:11, name:"葉子節點113"}, { id:114, pId:11, name:"葉子節點114"}, { id:12, pId:1, name:"父節點12 - 折疊"}, { id:121, pId:12, name:"葉子節點121"}, { id:122, pId:12, name:"葉子節點122"}, { id:123, pId:12, name:"葉子節點123"}, { id:124, pId:12, name:"葉子節點124"}, { id:13, pId:1, name:"父節點13 - 沒有子節點", isParent:true}, { id:2, pId:0, name:"父節點2 - 折疊"}, { id:21, pId:2, name:"父節點21 - 展開", open:true}, { id:211, pId:21, name:"葉子節點211"}, { id:212, pId:21, name:"葉子節點212"}, { id:213, pId:21, name:"葉子節點213"}, { id:214, pId:21, name:"葉子節點214"}, { id:22, pId:2, name:"父節點22 - 折疊"}, { id:221, pId:22, name:"葉子節點221"}, { id:222, pId:22, name:"葉子節點222"}, { id:223, pId:22, name:"葉子節點223"}, { id:224, pId:22, name:"葉子節點224"}, { id:23, pId:2, name:"父節點23 - 折疊"}, { id:231, pId:23, name:"葉子節點231"}, { id:232, pId:23, name:"葉子節點232"}, { id:233, pId:23, name:"葉子節點233"}, { id:234, pId:23, name:"葉子節點234"}, { id:3, pId:0, name:"父節點3 - 沒有子節點", isParent:true} ]; $(document).ready(function(){ $.fn.zTree.init($("#treeDemo"), setting, zNodes); }); //--> </SCRIPT>
JS方式二-標準json數據格式
<SCRIPT type="text/javascript"> <!-- var setting = { }; var zNodes =[ { name:"父節點1 - 展開", open:true, children: [ { name:"父節點11 - 折疊", children: [ { name:"葉子節點111"}, { name:"葉子節點112"}, { name:"葉子節點113"}, { name:"葉子節點114"} ]}, { name:"父節點12 - 折疊", children: [ { name:"葉子節點121"}, { name:"葉子節點122"}, { name:"葉子節點123"}, { name:"葉子節點124"} ]}, { name:"父節點13 - 沒有子節點", isParent:true} ]}, { name:"父節點2 - 折疊", children: [ { name:"父節點21 - 展開", open:true, children: [ { name:"葉子節點211"}, { name:"葉子節點212"}, { name:"葉子節點213"}, { name:"葉子節點214"} ]}, { name:"父節點22 - 折疊", children: [ { name:"葉子節點221"}, { name:"葉子節點222"}, { name:"葉子節點223"}, { name:"葉子節點224"} ]}, { name:"父節點23 - 折疊", children: [ { name:"葉子節點231"}, { name:"葉子節點232"}, { name:"葉子節點233"}, { name:"葉子節點234"} ]} ]}, { name:"父節點3 - 沒有子節點", isParent:true} ]; $(document).ready(function(){ $.fn.zTree.init($("#treeDemo"), setting, zNodes); }); //--> </SCRIPT>
異步zTree加載
前面的配置相同,在此不再闡述.主要是js不同.
異步JS
var setting = { //可勾選 check: { enable: true }, data : { simpleData : { enable : true } } }; function initTree(){ var payFreq = $("#payFreq").val(); var fyType = $('#fyType').val(); var setHzType = $('#setHzType').val(); $.ajax({ url : "/demo/initTree", data : {payFreq:payFreq, fyType:fyType, setHzType:setHzType }, success: function(object){ var nodes = ""; //拼接simple格式的json字符串 $.each(object.data, function(i,item) { nodes+="{id:'"+item.id+"', pId:'"+item.pid+"', name:'"+item.name+"', isParent:'"+item.isParent+"'},"; }); var zNodes = "["+nodes+"]"; var json = eval('(' + zNodes + ')'); //console.log(json); zTreeInit(json); } }); } /* 初始化樹 */ function zTreeInit(json) { $.fn.zTree.init($("#treeDemo"), setting, json); var zTree = $.fn.zTree.getZTreeObj("treeDemo"); //全部展開 zTree.expandAll(true); //Y代表勾選時,N代表取消勾選 p代表父節點,s代表字節點 setting.check.chkboxType = { "Y" : "ps", "N" : "ps" }; }
java代碼
Controller層
@RequestMapping("initTree") @ResponseBody public DataMessage initTree(String setHzType,String payFreq,String fyType){ params.put("setHzType", setHzType); params.put("fyType", fyType); params.put("fkmattr_xt", fkmattr_xt); //獲取treeinfo列表 List<TreeInfo> treeInfos = feeTransferService.initTree(params); return DataMessage.successData(treeInfos); }
tree實體類
public class TreeInfo { private String id; private String pid; private String name; private String isParent; }
以上是“jQuery中zTree樹插件怎么用”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。