1. 效果
![](http://static.javashuo.com/static/loading.gif)
2. ztree的Demo (不交互後臺)
2.1 引入ztree環境
![](http://static.javashuo.com/static/loading.gif)
2.2 使用
使用ajax 拿到json格式數據 而後生成樹形菜單javascript
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>ztree-入門</title> <link rel="stylesheet" href="css/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.all.min.js" ></script> <script> // 參考文檔 https://blog.csdn.net/feng_zi_ye/article/details/80632671 $(function(){ // ajax請求 獲取json var zTreeNodes=""; var url="./jsonData.json"; var data="";//無參數的寫法 $.post(url,data,function(result){ zTreeNodes=result //第一個參數爲zTree的DOM容器,第二個爲zTree設置詳情可見官網api,第三個爲zTree的節點數據 var city = $.fn.zTree.init($("#tree"), setting, zTreeNodes); },"json"); //2. 設置setting var setting={ check: { enable: true, chkStyle: "checkbox" //顯示 checkbox 選擇框,默認checkbox可選擇值radio }, //添加回調函數 callback:{ onCheck:zTreeOnCheck } }; //3.給節點添加事件 function zTreeOnCheck(event, treeId, treeNode) { alert(treeNode.name);//彈出城市名字 } // });css </script> </head> <body> <ul id="tree" class="ztree" style="width:230px; overflow:auto;"></ul> </body> </html>html |
![](http://static.javashuo.com/static/loading.gif)
3. demo (交互後臺)
3.1 效果
![](http://static.javashuo.com/static/loading.gif)
3.2 過程
3.2.1 前端 發送ajax 請求
![](http://static.javashuo.com/static/loading.gif)
3.2.2 後臺
3.2.2.1 action 層
![](http://static.javashuo.com/static/loading.gif)
3.2.2.2 service層
![](http://static.javashuo.com/static/loading.gif)
3.2.2.3 mapper
![](http://static.javashuo.com/static/loading.gif)
3.2.2.4 數據庫
![](http://static.javashuo.com/static/loading.gif)
3.2.3 附件(源碼+數據庫)
碼雲上的:前端
https://gitee.com/Luck_Me/ztree_demojava
百度雲:jquery
連接:https://pan.baidu.com/s/1SoISHBcXsSiFarySAAQa1g
提取碼:aya6 git