Ztree树结构优化

master
RuoYi 6 years ago committed by Limy
parent b376661afd
commit 97cc85a3b8

@ -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;
}
}

@ -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;
}
/**

@ -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);
for (var i = 0; i < nodes.length; i++) {
if(_expandLevel > 0) {
tree.expandNode(nodes[i], true, false, false);
}
$.tree.selectByIdName(treeId, treeName, nodes[i]);
}
// 展开第二级节点
nodes = tree.getNodesByParam("level", 1);
var nodes = tree.getNodesByParam("level", options.expandLevel - 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]);
tree.expandNode(nodes[i], true, false, false);
}
// 展开第三级节点
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);
}
},
// 显示所有节点

@ -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);

@ -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);

@ -0,0 +1,104 @@
package com.ruoyi.common.base;
import java.io.Serializable;
/**
* Ztree
*
* @author ruoyi
*/
public class Ztree implements Serializable
{
private static final long serialVersionUID = 1L;
/** 节点ID */
private Long id;
/** 节点父ID */
private Long pId;
/** 节点名称 */
private String name;
/** 节点标题 */
private String title;
/** 是否勾选 */
private boolean checked = false;
/** 是否展开 */
private boolean open = false;
/** 是否能勾选 */
private boolean nocheck = false;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public Long getpId()
{
return pId;
}
public void setpId(Long pId)
{
this.pId = pId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public boolean isChecked()
{
return checked;
}
public void setChecked(boolean checked)
{
this.checked = checked;
}
public boolean isOpen()
{
return open;
}
public void setOpen(boolean open)
{
this.open = open;
}
public boolean isNocheck()
{
return nocheck;
}
public void setNocheck(boolean nocheck)
{
this.nocheck = nocheck;
}
}

@ -1,7 +1,7 @@
package com.ruoyi.system.service;
import java.util.List;
import java.util.Map;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysRole;
@ -26,7 +26,7 @@ public interface ISysDeptService
* @param dept
* @return
*/
public List<Map<String, Object>> selectDeptTree(SysDept dept);
public List<Ztree> selectDeptTree(SysDept dept);
/**
* ID
@ -34,7 +34,7 @@ public interface ISysDeptService
* @param role
* @return
*/
public List<Map<String, Object>> roleDeptTreeData(SysRole role);
public List<Ztree> roleDeptTreeData(SysRole role);
/**
*

@ -3,6 +3,7 @@ package com.ruoyi.system.service;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
@ -51,14 +52,14 @@ public interface ISysMenuService
* @param role
* @return
*/
public List<Map<String, Object>> roleMenuTreeData(SysRole role);
public List<Ztree> roleMenuTreeData(SysRole role);
/**
*
*
* @return
*/
public List<Map<String, Object>> menuTreeData();
public List<Ztree> menuTreeData();
/**
*

@ -1,12 +1,11 @@
package com.ruoyi.system.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
@ -47,12 +46,11 @@ public class SysDeptServiceImpl implements ISysDeptService
*/
@Override
@DataScope(tableAlias = "d")
public List<Map<String, Object>> selectDeptTree(SysDept dept)
public List<Ztree> selectDeptTree(SysDept dept)
{
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<SysDept> deptList = deptMapper.selectDeptList(dept);
trees = getTrees(deptList, false, null);
return trees;
List<Ztree> ztrees = initZtree(deptList);
return ztrees;
}
/**
@ -62,56 +60,63 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return
*/
@Override
public List<Map<String, Object>> roleDeptTreeData(SysRole role)
public List<Ztree> roleDeptTreeData(SysRole role)
{
Long roleId = role.getRoleId();
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<Ztree> ztrees = new ArrayList<Ztree>();
List<SysDept> deptList = selectDeptList(new SysDept());
if (StringUtils.isNotNull(roleId))
{
List<String> roleDeptList = deptMapper.selectRoleDeptTree(roleId);
trees = getTrees(deptList, true, roleDeptList);
ztrees = initZtree(deptList, roleDeptList);
}
else
{
trees = getTrees(deptList, false, null);
ztrees = initZtree(deptList);
}
return trees;
return ztrees;
}
/**
*
*
* @param deptList
* @return
*/
public List<Ztree> initZtree(List<SysDept> deptList)
{
return initZtree(deptList, null);
}
/**
*
*
* @param deptList
* @param isCheck
* @param roleDeptList
* @return
* @return
*/
public List<Map<String, Object>> getTrees(List<SysDept> deptList, boolean isCheck, List<String> roleDeptList)
public List<Ztree> initZtree(List<SysDept> deptList, List<String> roleDeptList)
{
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<Ztree> ztrees = new ArrayList<Ztree>();
boolean isCheck = StringUtils.isNotNull(roleDeptList);
for (SysDept dept : deptList)
{
if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
{
Map<String, Object> deptMap = new HashMap<String, Object>();
deptMap.put("id", dept.getDeptId());
deptMap.put("pId", dept.getParentId());
deptMap.put("name", dept.getDeptName());
deptMap.put("title", dept.getDeptName());
Ztree ztree = new Ztree();
ztree.setId(dept.getDeptId());
ztree.setpId(dept.getParentId());
ztree.setName(dept.getDeptName());
ztree.setTitle(dept.getDeptName());
if (isCheck)
{
deptMap.put("checked", roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
}
else
{
deptMap.put("checked", false);
ztree.setChecked(roleDeptList.contains(dept.getDeptId() + dept.getDeptName()));
}
trees.add(deptMap);
ztrees.add(ztree);
}
}
return trees;
return ztrees;
}
/**

@ -3,16 +3,15 @@ package com.ruoyi.system.service.impl;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.base.Ztree;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysMenu;
@ -110,21 +109,21 @@ public class SysMenuServiceImpl implements ISysMenuService
* @return
*/
@Override
public List<Map<String, Object>> roleMenuTreeData(SysRole role)
public List<Ztree> roleMenuTreeData(SysRole role)
{
Long roleId = role.getRoleId();
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<Ztree> ztrees = new ArrayList<Ztree>();
List<SysMenu> menuList = menuMapper.selectMenuAll();
if (StringUtils.isNotNull(roleId))
{
List<String> roleMenuList = menuMapper.selectMenuTree(roleId);
trees = getTrees(menuList, true, roleMenuList, true);
ztrees = initZtree(menuList, roleMenuList, true);
}
else
{
trees = getTrees(menuList, false, null, true);
ztrees = initZtree(menuList, null, true);
}
return trees;
return ztrees;
}
/**
@ -133,12 +132,11 @@ public class SysMenuServiceImpl implements ISysMenuService
* @return
*/
@Override
public List<Map<String, Object>> menuTreeData()
public List<Ztree> menuTreeData()
{
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<SysMenu> menuList = menuMapper.selectMenuAll();
trees = getTrees(menuList, false, null, false);
return trees;
List<Ztree> ztrees = initZtree(menuList);
return ztrees;
}
/**
@ -165,33 +163,39 @@ public class SysMenuServiceImpl implements ISysMenuService
*
*
* @param menuList
* @param isCheck
* @return
*/
public List<Ztree> initZtree(List<SysMenu> menuList)
{
return initZtree(menuList, null, false);
}
/**
*
*
* @param menuList
* @param roleMenuList
* @param permsFlag
* @return
* @return
*/
public List<Map<String, Object>> getTrees(List<SysMenu> menuList, boolean isCheck, List<String> roleMenuList,
boolean permsFlag)
public List<Ztree> initZtree(List<SysMenu> menuList, List<String> roleMenuList, boolean permsFlag)
{
List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
List<Ztree> ztrees = new ArrayList<Ztree>();
boolean isCheck = StringUtils.isNotNull(roleMenuList);
for (SysMenu menu : menuList)
{
Map<String, Object> deptMap = new HashMap<String, Object>();
deptMap.put("id", menu.getMenuId());
deptMap.put("pId", menu.getParentId());
deptMap.put("name", transMenuName(menu, roleMenuList, permsFlag));
deptMap.put("title", menu.getMenuName());
Ztree ztree = new Ztree();
ztree.setId(menu.getMenuId());
ztree.setpId(menu.getParentId());
ztree.setName(transMenuName(menu, roleMenuList, permsFlag));
ztree.setTitle(menu.getMenuName());
if (isCheck)
{
deptMap.put("checked", roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
}
else
{
deptMap.put("checked", false);
ztree.setChecked(roleMenuList.contains(menu.getMenuId() + menu.getPerms()));
}
trees.add(deptMap);
ztrees.add(ztree);
}
return trees;
return ztrees;
}
public String transMenuName(SysMenu menu, List<String> roleMenuList, boolean permsFlag)

Loading…
Cancel
Save