目录
一、前言
二、读取数据表
三、要用到的工具类
四、编写后台代码
五、前台拼接HTML代码
六、测试结果
在上篇文章我们一起学习了有关LayUI入门的一些相关知识,了解了LayUI的一些特点以及如何使用;而这篇文章我们就来看看如何用LayUI动态的实现左侧菜单栏数据!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="/common/head.jsp" %>
内容主体区域。记得修改 layui.css 和 js 的路径
我们首先要来查询一下我们要绑定的数据表!这里用到了我们之前编写的通用查询~
//查询t_oa_permission表中的数据
public List list(Permission permission, PageBean pageBean) throws Exception {
String sql = "select * from t_oa_permission";
return super.executeQuery(sql, Permission.class, pageBean);
}
接着通过我们查询到的菜单数据可以分析得到我们的菜单数据需要具备一种父子关系,所以,我们封装了一个工具类,用来将这些数据转换为具有父子关系的数据!
我们还需要根据这个菜单数据建立一个辅助类:
package com.leaf.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class TreeVo {
/**
* 节点ID
*/
private String id;
/**
* 显示节点文本
*/
private String text;
/**
* 节点状态,open closed
*/
private Map state;
/**
* 节点是否被选中 true false
*/
private boolean checked = false;
/**
* 节点属性
*/
private Map attributes;
/**
* 节点的子节点
*/
private List> children = new ArrayList>();
/**
* 父ID
*/
private String parentId;
/**
* 是否有父节点
*/
private boolean hasParent = false;
/**
* 是否有子节点
*/
private boolean hasChildren = false;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Map getState() {
return state;
}
public void setState(Map state) {
this.state = state;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public Map getAttributes() {
return attributes;
}
public void setAttributes(Map attributes) {
this.attributes = attributes;
}
public List> getChildren() {
return children;
}
public void setChildren(List> children) {
this.children = children;
}
public boolean isHasParent() {
return hasParent;
}
public void setHasParent(boolean isParent) {
this.hasParent = isParent;
}
public boolean isHasChildren() {
return hasChildren;
}
public void setChildren(boolean isChildren) {
this.hasChildren = isChildren;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public TreeVo(String id, String text, Map state, boolean checked, Map attributes,
List> children, boolean isParent, boolean isChildren, String parentID) {
super();
this.id = id;
this.text = text;
this.state = state;
this.checked = checked;
this.attributes = attributes;
this.children = children;
this.hasParent = isParent;
this.hasChildren = isChildren;
this.parentId = parentID;
}
public TreeVo() {
super();
}
}
这里就直接放上这个工具类的代码了:
package com.leaf.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BuildTree {
/**
* 默认-1为顶级节点
* @param nodes
* @param
* @return
*/
public static TreeVo build(List> nodes) {
if (nodes == null) {
return null;
}
List> topNodes = new ArrayList>();
for (TreeVo children : nodes) {
String pid = children.getParentId();
if (pid == null || "-1".equals(pid)) {
topNodes.add(children);
continue;
}
for (TreeVo parent : nodes) {
String id = parent.getId();
if (id != null && id.equals(pid)) {
parent.getChildren().add(children);
children.setHasParent(true);
parent.setChildren(true);
continue;
}
}
}
TreeVo root = new TreeVo();
if (topNodes.size() == 1) {
root = topNodes.get(0);
} else {
root.setId("000");
root.setParentId("-1");
root.setHasParent(false);
root.setChildren(true);
root.setChecked(true);
root.setChildren(topNodes);
root.setText("顶级节点");
Map state = new HashMap<>(16);
state.put("opened", true);
root.setState(state);
}
return root;
}
/**
* 指定idparam为顶级节点
* @param nodes
* @param idParam
* @param
* @return
*/
public static List> buildList(List> nodes, String idParam) {
if (nodes == null) {
return null;
}
List> topNodes = new ArrayList>();
for (TreeVo children : nodes) {
String pid = children.getParentId();
if (pid == null || idParam.equals(pid)) {
topNodes.add(children);
continue;
}
for (TreeVo parent : nodes) {
String id = parent.getId();
if (id != null && id.equals(pid)) {
parent.getChildren().add(children);
children.setHasParent(true);
parent.setChildren(true);
continue;
}
}
}
return topNodes;
}
}
接着我们就可以来完善一下之前写的菜单数据访问层啦,编写一个方法能够让查询到的菜单数据转换成符合前台展示的格式!
public List> menus(Permission permission, PageBean pageBean) throws Exception {
//定义一个数据节点的集合
List> trees = new ArrayList>();
//调用查询数据表的方法
List list = this.list(permission, pageBean);
//循环遍历
for (Permission pm : list) {
//定义一个数据节点类
TreeVo vo = new TreeVo<>();
//赋值
vo.setId(pm.getId()+"");
vo.setText(pm.getName());//节点名称
vo.setParentId(pm.getPid()+"");
trees.add(vo);
}
//调用工具类
return BuildTree.buildList(trees, "-1");
}
我们还要编写一个处理前台请求的PermissionAction
package com.leaf.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.leaf.dao.PermissionDao;
import com.leaf.entity.Permission;
import com.leaf.mvc.ActionSupport;
import com.leaf.mvc.ModelDriven;
import com.leaf.util.ResponseUtil;
import com.leaf.util.TreeVo;
public class PermissionAction extends ActionSupport implements ModelDriven {
private Permission permission = new Permission();
PermissionDao pd = new PermissionDao();
@Override
public Permission getModel() {
return permission;
}
public String menus(HttpServletRequest req, HttpServletResponse resp) {
try {
List> list = pd.menus(null, null);
//往前台响应树形结构的数据
ResponseUtil.writeJson(resp, list);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我们再编写好配置:
到这一步我们就可以到前台接收到返回的处理好的数据,并且利用LayUI的each循环拼接HTML代码展示啦~
当我们拼接好前台的代码后就可以运行测试一下啦!
OK,今天Leaf带来的LayUI动态菜单的实现的相关知识就到这里啦,我们下次见!!!