本篇文章是接著Spring Boot 入門(五):整合 AOP 進行日誌管理寫的,主要整合了樹形圖,在部門列表或者許可權列表中,樹形圖經常被用上。主要是根據相應的 API 憑藉 html 字串
1.treetable
頁面程式碼如下:
<link href="/plugins/treeTable/themes/default/treeTable.css" rel="stylesheet" type="text/css" />
<script src="/plugins/treeTable/jquery.treeTable.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {});
</script>
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header with-border">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3 class="box-title">選擇部門</h3>
</div>
<div class="box-body">
<table id="tcXzqh_tab" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>部門編號</th>
<th>部門名稱</th>
<th>上級部門編號</th>
<th>上級部門名稱</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
var tcXzqh_tab;
function loadTcXzqhTable(){
$.ajax({
url: '/admin/dept/spage',
type: 'post',
dataType: 'json',
success: function (data) {
if(data !=null){
var html = "";
for (var int = 0; int < data.resultData.length; int++) {
var item = data.resultData[int];
if(item.hasChild == true){
html+=
"<tr id='"+item.xzqhbm+"' pId='"+item.pXzqhbm+"' haschild='true' ondblclick=_callback('" + item.xzqhbm + ":" + item.xzqh + "')>";
}else{
html+=
"<tr id='"+item.xzqhbm+"' pId='"+item.pXzqhbm+"' ondblclick=_callback('" + item.xzqhbm + ":" + item.xzqh + "')>";
}
html+=
"<td nowrap>"+item.xzqhbm+"</td>"+
"<td nowrap>"+item.xzqh+"</td>"+
"<td nowrap>"+item.pXzqhbm+"</td>"+
"<td nowrap>"+item.pXzqh +"</td>"+
"</tr>";
}
$("#tcXzqh_tab tbody").html(html);
initTcXzqh_treeTable();
}
//$("#loading").hide();
}
});
}
function initTcXzqh_treeTable(){
var option = {
/*theme:'vsStyle',*//*這裡的主題和引入的CSS有關*/
expandLevel : 2,
beforeExpand : function($treeTable, id) {
//alert(id);
//判斷id是否已經有了孩子節點,如果有了就不再載入,這樣就可以起到快取的作用
if ($('.' + id, $treeTable).length) { return; }
//這裡的html可以是ajax請求
openHandleLoading();
var childHtml="";
$.ajax({
url: '/admin/dept/expand?parentId='+id,
type: 'post',
dataType: 'json',
success: function (data) {
//alert(data);
if(data !=null){
//var html = "";
for (var int = 0; int < data.resultData.length; int++) {
var item = data.resultData[int];
if(item.hasChild == true){
childHtml+=
"<tr id='"+item.xzqhbm+"' pId='"+item.pXzqhbm+"' haschild='true' ondblclick=_callback('" + item.xzqhbm + ":" + item.xzqh + "')>";
}else{
childHtml+=
"<tr id='"+item.xzqhbm+"' pId='"+item.pXzqhbm+"' ondblclick=_callback('" + item.xzqhbm + ":" + item.xzqh + "')>";
}
childHtml+=
"<td nowrap>"+item.xzqhbm+"</td>"+
"<td nowrap>"+item.xzqh+"</td>"+
"<td nowrap>"+item.pXzqhbm+"</td>"+
"<td nowrap>"+item.pXzqh+"</td>"+
"</tr>";
}
}
$treeTable.addChilds(childHtml);
closeHandleLoading();
}
});
},
onSelect : function($treeTable, id) {
//console.log('onSelect:' + id);
}
};
$("#tcXzqh_tab").treeTable(option).show();
//$("#tcXzqh_tab").treeTable({expandLevel : 2,column:0}).show();
}
function _callback(chooseValue) {
/*alert(chooseValue);*/
$("#${inputId!}").val(chooseValue);
$("#lgModal2").modal("hide");
}
$(function () {
loadTcXzqhTable();
});
</script>
複製程式碼
首先引入相應的 JS 檔案和樣式檔案
然後初始化列表介面 此部分對應的後臺只需要返回一個 List 集合即可,用於初始化介面,其效果如下: 由於需要在展開節點的時候需要動態載入,所以需要實現 expend 方法 在展開節點的時候,需要根據父節點去查詢其下級幾點的資訊,並返回給頁面,實現動態載入樹形圖,其效果如下:2.ztree
引入 JS 和 CSS 樣式
<link rel="stylesheet" href="/plugins/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="/plugins/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/plugins/ztree/js/jquery.ztree.excheck.js"></script>
複製程式碼
編寫一個div,用於存放樹形圖
<div class="form-group clearfix">
<label id="parentdmLabel" class="col-sm-2 control-label" style="text-align:right">選單許可權</label>
<div class="col-sm-10">
<div id="treeDemoUpdate" class="ztree"></div>
</div>
</div>
複製程式碼
JS部分程式碼
<SCRIPT type="text/javascript">
var setting = {
check: {
enable: true
},
data: {
simpleData: {
enable: true
}
},
callback: {
beforeCheck:true,
onCheck:zTreeOnClick
}
};
var zNodes ;
var code;
function setCheck() {
var zTree = $.fn.zTree.getZTreeObj("treeDemoUpdate"),
type = {"Y": "ps", "N": "ps"};
zTree.setting.check.chkboxType = type;
showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
}
function showCode(str) {
if (!code) code = $("#code");
code.empty();
code.append("<li>" + str + "</li>");
}
$(document).ready(function () {
$.ajax({
url: '/admin/role/roleUpdateTree?roleId='+${tRole.roleId},
type: 'post',
async: false ,
dataType: 'json',
success: function (data) {
zNodes=data;
}
});
$.fn.zTree.init($("#treeDemoUpdate"), setting, zNodes);
setCheck();
/* $("#py").bind("change", setCheck);
$("#sy").bind("change", setCheck);
$("#pn").bind("change", setCheck);
$("#sn").bind("change", setCheck);*/
});
function zTreeOnClick(event, treeId, treeNode) {
var treeObj = $.fn.zTree.getZTreeObj("treeDemoUpdate");
var nodes = treeObj.getCheckedNodes(true);
var ids="";
for (var i = 0; i < nodes.length; i++) {
ids+=nodes[i].id+",";
}
//console.log(ids.substring(0,ids.length-1));
$("#permissionIds").val(ids.substring(0,ids.length-1));
}
</SCRIPT>
複製程式碼
效果如下: