Ztree树结构优化

This commit is contained in:
RuoYi
2019-03-01 15:26:04 +08:00
parent 161c0f1a67
commit 1d01d20b49
10 changed files with 234 additions and 120 deletions

View File

@ -1,7 +1,6 @@
package com.ruoyi.web.controller.system;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -11,15 +10,16 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.framework.web.base.BaseController;
/**
* 部门信息
@ -147,10 +147,10 @@ public class SysDeptController extends BaseController
*/
@GetMapping("/treeData")
@ResponseBody
public List<Map<String, Object>> treeData()
public List<Ztree> treeData()
{
List<Map<String, Object>> tree = deptService.selectDeptTree(new SysDept());
return tree;
List<Ztree> ztrees = deptService.selectDeptTree(new SysDept());
return ztrees;
}
/**
@ -158,9 +158,9 @@ public class SysDeptController extends BaseController
*/
@GetMapping("/roleDeptTreeData")
@ResponseBody
public List<Map<String, Object>> deptTreeData(SysRole role)
public List<Ztree> deptTreeData(SysRole role)
{
List<Map<String, Object>> tree = deptService.roleDeptTreeData(role);
return tree;
List<Ztree> ztrees = deptService.roleDeptTreeData(role);
return ztrees;
}
}

View File

@ -1,7 +1,6 @@
package com.ruoyi.web.controller.system;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -13,12 +12,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.framework.web.base.BaseController;
/**
* 菜单信息
@ -154,10 +154,10 @@ public class SysMenuController extends BaseController
*/
@GetMapping("/roleMenuTreeData")
@ResponseBody
public List<Map<String, Object>> roleMenuTreeData(SysRole role)
public List<Ztree> roleMenuTreeData(SysRole role)
{
List<Map<String, Object>> tree = menuService.roleMenuTreeData(role);
return tree;
List<Ztree> ztrees = menuService.roleMenuTreeData(role);
return ztrees;
}
/**
@ -165,10 +165,10 @@ public class SysMenuController extends BaseController
*/
@GetMapping("/menuTreeData")
@ResponseBody
public List<Map<String, Object>> menuTreeData(SysRole role)
public List<Ztree> menuTreeData(SysRole role)
{
List<Map<String, Object>> tree = menuService.menuTreeData();
return tree;
List<Ztree> ztrees = menuService.menuTreeData();
return ztrees;
}
/**

View File

@ -842,48 +842,50 @@
_lastValue: {},
// 初始化树结构
init: function(options) {
var defaults = {
id: "tree", // 属性ID
expandLevel: 0, // 展开等级节点
view: {
selectedMulti: false, // 设置是否允许同时选中多个节点
nameIsHTML: true // 设置 name 属性是否支持 HTML 脚本
},
check: {
enable: false, // 置 zTree 的节点上是否显示 checkbox / radio
nocheckInherit: true, // 设置子节点是否自动继承
},
data: {
key: {
title: "title" // 节点数据保存节点提示信息的属性名称
},
simpleData: {
enable: true // true / false 分别表示 使用 / 不使用 简单数据模式
}
},
};
var options = $.extend(defaults, options);
$.tree._option = options;
// 属性ID
var _id = $.common.isEmpty(options.id) ? "tree" : options.id;
// 展开等级节点
var _expandLevel = $.common.isEmpty(options.expandLevel) ? 0 : options.expandLevel;
// 树结构初始化加载
var setting = {
check: options.check,
view: { selectedMulti: false, nameIsHTML: true },
data: { key: { title: "title" }, simpleData: { enable: true } },
callback: { onClick: options.onClick }
};
var setting = {
callback: {
onClick: options.onClick, // 用于捕获节点被点击的事件回调函数
onCheck: options.onCheck, // 用于捕获 checkbox / radio 被勾选 或 取消勾选的事件回调函数
onDblClick: options.onDblClick // 用于捕获鼠标双击之后的事件回调函数
},
check: options.check,
view: options.view,
data: options.data
};
$.get(options.url, function(data) {
var treeName = $("#treeName").val();
var treeId = $("#treeId").val();
tree = $.fn.zTree.init($("#" + _id), setting, data);
tree = $.fn.zTree.init($("#" + options.id), setting, data);
$._tree = tree;
// 展开第一级节点
var nodes = tree.getNodesByParam("level", 0);
var nodes = tree.getNodesByParam("level", options.expandLevel - 1);
for (var i = 0; i < nodes.length; i++) {
if(_expandLevel > 0) {
tree.expandNode(nodes[i], true, false, false);
}
$.tree.selectByIdName(treeId, treeName, nodes[i]);
tree.expandNode(nodes[i], true, false, false);
}
// 展开第二级节点
nodes = tree.getNodesByParam("level", 1);
for (var i = 0; i < nodes.length; i++) {
if(_expandLevel > 1) {
tree.expandNode(nodes[i], true, false, false);
}
$.tree.selectByIdName(treeId, treeName, nodes[i]);
}
// 展开第三级节点
nodes = tree.getNodesByParam("level", 2);
for (var i = 0; i < nodes.length; i++) {
if(_expandLevel > 2) {
tree.expandNode(nodes[i], true, false, false);
}
$.tree.selectByIdName(treeId, treeName, nodes[i]);
}
}, null, null, "正在加载,请稍后...");
var node = tree.getNodesByParam("id", treeId, null)[0];
$.tree.selectByIdName(treeId, node);
});
},
// 搜索节点
searchNode: function() {
@ -905,11 +907,9 @@
$.tree.updateNodes($._tree.getNodesByParamFuzzy("name", value));
},
// 根据Id和Name选中指定节点
selectByIdName: function(treeId, treeName, node) {
if ($.common.isNotEmpty(treeName) && $.common.isNotEmpty(treeId)) {
if (treeId == node.id && treeName == node.name) {
$._tree.selectNode(node, true);
}
selectByIdName: function(treeId, node) {
if ($.common.isNotEmpty(treeId) && treeId == node.id) {
$._tree.selectNode(node, true);
}
},
// 显示所有节点

View File

@ -58,7 +58,7 @@
var options = {
id: "menuTrees",
url: url,
check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
check: { enable: true },
expandLevel: 0
};
$.tree.init(options);

View File

@ -59,7 +59,7 @@
var options = {
id: "menuTrees",
url: url,
check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
check: { enable: true },
expandLevel: 0
};
$.tree.init(options);