Spring Boot 入門(六):集成 treetable 和 zTree 實現樹形圖

 

本篇文章是接着Spring Boot 入門(五):集成 AOP 進行日誌管理寫的,主要集成了樹形圖,在部門列表或者權限列表中,樹形圖常常被用上。主要是根據相應的 API 憑藉 html 字符串javascript

1.treetable

  1 <link href="/plugins/treeTable/themes/default/treeTable.css" rel="stylesheet" type="text/css" />
  2 <script src="/plugins/treeTable/jquery.treeTable.min.js" type="text/javascript"></script>
  3 <script type="text/javascript">
  4     $(document).ready(function() {});
  5 </script>
  6     
  7 <div class="row">
  8     <div class="col-xs-12">
  9        <div class="box">
 10          <div class="box-header  with-border">
 11             <button type="button" class="close" data-dismiss="modal" aria-label="Close">
 12                 <span aria-hidden="true">&times</span>
 13             </button>
 14            <h3 class="box-title">選擇部門</h3>
 15          </div>
 16          <div class="box-body">
 17             <table id="tcXzqh_tab" class="table table-striped table-bordered table-hover">
 18                <thead>
 19                    <tr>
 20                     <th>部門編號</th>
 21                     <th>部門名稱</th>
 22                     <th>上級部門編號</th>
 23                     <th>上級部門名稱</th>
 24                    </tr>
 25                </thead>
 26                <tbody>
 27                </tbody>
 28            </table>
 29          </div>
 30        </div>
 31     </div>
 32 </div>
 33   
 34 <script>
 35 var tcXzqh_tab;
 36 function loadTcXzqhTable(){
 37 
 38   $.ajax({
 39         url: '/admin/dept/spage',
 40         type: 'post',
 41         dataType: 'json',
 42         success: function (data) {
 43             if(data !=null){
 44                 var html = "";
 45                 for (var int = 0; int < data.resultData.length; int++) {
 46                     var item = data.resultData[int];
 47                     if(item.hasChild == true){
 48                         html+=
 49                                 "<tr id='"+item.xzqhbm+"' pId='"+item.pXzqhbm+"' haschild='true' ondblclick=_callback('" + item.xzqhbm + ":" + item.xzqh + "')>";
 50                     }else{
 51                         html+=
 52                                 "<tr id='"+item.xzqhbm+"' pId='"+item.pXzqhbm+"' ondblclick=_callback('" + item.xzqhbm + ":" + item.xzqh + "')>";
 53                     }
 54                     html+=
 55                             "<td nowrap>"+item.xzqhbm+"</td>"+
 56                             "<td nowrap>"+item.xzqh+"</td>"+
 57                             "<td nowrap>"+item.pXzqhbm+"</td>"+
 58                             "<td nowrap>"+item.pXzqh +"</td>"+
 59                         "</tr>";
 60                 }
 61                 $("#tcXzqh_tab tbody").html(html);
 62                 initTcXzqh_treeTable();
 63             }
 64             //$("#loading").hide();
 65         }
 66     });
 67  }
 68 function initTcXzqh_treeTable(){
 69         var option = {
 70              /*theme:'vsStyle',*//*這裏的主題和引入的CSS有關*/
 71              expandLevel : 2,
 72              beforeExpand : function($treeTable, id) {
 73                  //alert(id);
 74                  //判斷id是否已經有了孩子節點,若是有了就再也不加載,這樣就能夠起到緩存的做用
 75                  if ($('.' + id, $treeTable).length) { return; }
 76                  //這裏的html能夠是ajax請求
 77                  openHandleLoading();
 78                  var childHtml="";
 79                      $.ajax({
 80                             url: '/admin/dept/expand?parentId='+id,
 81                             type: 'post',
 82                             dataType: 'json',
 83                             success: function (data) {
 84                                 //alert(data);
 85                                 if(data !=null){
 86                                     //var html = "";
 87                                     for (var int = 0; int < data.resultData.length; int++) {
 88                                         var item = data.resultData[int];
 89                                         
 90                                         if(item.hasChild == true){
 91                                             childHtml+= 
 92                                             "<tr id='"+item.xzqhbm+"' pId='"+item.pXzqhbm+"' haschild='true' ondblclick=_callback('" + item.xzqhbm + ":" + item.xzqh + "')>";
 93                                             
 94                                         }else{
 95                                             childHtml+= 
 96                                             "<tr id='"+item.xzqhbm+"' pId='"+item.pXzqhbm+"' ondblclick=_callback('" + item.xzqhbm + ":" + item.xzqh + "')>";
 97                                             
 98                                         }
 99                                         childHtml+= 
100                                                 "<td nowrap>"+item.xzqhbm+"</td>"+
101                                                 "<td nowrap>"+item.xzqh+"</td>"+
102                                                 "<td nowrap>"+item.pXzqhbm+"</td>"+
103                                                 "<td nowrap>"+item.pXzqh+"</td>"+
104                                             "</tr>";
105                                     }
106                     
107                                 }
108                                  $treeTable.addChilds(childHtml);
109                                  closeHandleLoading();
110                             }
111                           });
112              },
113             onSelect : function($treeTable, id) {
114                 //console.log('onSelect:' + id);
115             }
116 
117          };
118         
119         $("#tcXzqh_tab").treeTable(option).show();
120         //$("#tcXzqh_tab").treeTable({expandLevel : 2,column:0}).show();
121  }
122 function _callback(chooseValue) {
123      /*alert(chooseValue);*/
124     $("#${inputId!}").val(chooseValue);
125     $("#lgModal2").modal("hide");
126 }
127 $(function () {
128       loadTcXzqhTable();
129 });
130 
131 </script>

 

首先引入相應的 JS 文件和樣式文件css

 
而後初始化列表界面
此部分對應的後臺只須要返回一個 List 集合便可,用於初始化界面,其效果以下:

 

因爲須要在展開節點的時候須要動態加載,因此須要實現 expend 方法
在展開節點的時候,須要根據父節點去查找其下級幾點的信息,並返回給頁面,實現動態加載樹形圖,其效果以下:

 

2.ztree

引入 JS 和 CSS 樣式html

1 <link rel="stylesheet" href="/plugins/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
2 <script type="text/javascript" src="/plugins/ztree/js/jquery.ztree.core.js"></script>
3 <script type="text/javascript" src="/plugins/ztree/js/jquery.ztree.excheck.js"></script>
 

編寫一個div,用於存放樹形圖java

1  <div class="form-group clearfix">
2           <label id="parentdmLabel" class="col-sm-2 control-label" style="text-align:right">菜單權限</label>
3           <div class="col-sm-10">
4               <div id="treeDemoUpdate" class="ztree"></div>
5           </div>
6   </div>

JS部分代碼node

 1 <SCRIPT type="text/javascript">
 2     var setting = {
 3         check: {
 4             enable: true
 5         },
 6         data: {
 7             simpleData: {
 8                 enable: true
 9             }
10         },
11         callback: {
12             beforeCheck:true,
13             onCheck:zTreeOnClick
14         }
15     };
16     var zNodes ;
17     var code;
18     function setCheck() {
19         var zTree = $.fn.zTree.getZTreeObj("treeDemoUpdate"),
20         type = {"Y": "ps", "N": "ps"};
21         zTree.setting.check.chkboxType = type;
22         showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
23     }
24     function showCode(str) {
25         if (!code) code = $("#code");
26         code.empty();
27         code.append("<li>" + str + "</li>");
28     }
29     $(document).ready(function () {
30        $.ajax({
31             url: '/admin/role/roleUpdateTree?roleId='+${tRole.roleId},
32             type: 'post',
33             async: false ,
34             dataType: 'json',
35             success: function (data) {
36                 zNodes=data;
37             }
38         });
39         $.fn.zTree.init($("#treeDemoUpdate"), setting, zNodes);
40         setCheck();
41         /* $("#py").bind("change", setCheck);
42          $("#sy").bind("change", setCheck);
43          $("#pn").bind("change", setCheck);
44          $("#sn").bind("change", setCheck);*/
45     });
46     
47     function zTreeOnClick(event, treeId, treeNode) {
48         var treeObj = $.fn.zTree.getZTreeObj("treeDemoUpdate");
49         var nodes = treeObj.getCheckedNodes(true);
50         var ids="";
51         for (var i = 0; i < nodes.length; i++) {
52             ids+=nodes[i].id+",";
53         }
54         //console.log(ids.substring(0,ids.length-1));
55         $("#permissionIds").val(ids.substring(0,ids.length-1));
56     }
57 </SCRIPT>

 

 
效果以下:

 

相關文章
相關標籤/搜索