Layui之动态树(树形菜单)详解2

简介: Layui之动态树(树形菜单)详解2

三.详细步骤

1.左侧选项卡实体类

private long id;
  private String name;
  private String description;
  private String url;
  private long pid;
  private int ismenu;//控制系统权限是菜单还是按钮
  private long displayno;//菜单排序字段
  public Permission() {
    // TODO Auto-generated constructor stub
  }
  public long getId() {
    return id;
  }
  public void setId(long id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getDescription() {
    return description;
  }
  public void setDescription(String description) {
    this.description = description;
  }
  public String getUrl() {
    return url;
  }
  public void setUrl(String url) {
    this.url = url;
  }
  public long getPid() {
    return pid;
  }
  public void setPid(long pid) {
    this.pid = pid;
  }
  public int getIsmenu() {
    return ismenu;
  }
  public void setIsmenu(int ismenu) {
    this.ismenu = ismenu;
  }
  public long getDisplayno() {
    return displayno;
  }
  public void setDisplayno(long displayno) {
    this.displayno = displayno;
  }
  public Permission(long id, String name, String description, String url, long pid, int ismenu, long displayno) {
    super();
    this.id = id;
    this.name = name;
    this.description = description;
    this.url = url;
    this.pid = pid;
    this.ismenu = ismenu;
    this.displayno = displayno;
  }
  @Override
  public String toString() {
    return "Permission [id=" + id + ", name=" + name + ", description=" + description + ", url=" + url + ", pid="
        + pid + ", ismenu=" + ismenu + ", displayno=" + displayno + "]";
  }

2.通过dao方法获取数据

1.1 通过permissiondao获取数据库表中所有菜单栏数据

public List<Permission> list(Permission permission, PageBean pageBean) throws Exception {
    String sql = "select * from t_oa_permission";
    return super.executeQuery(sql, Permission.class, pageBean);
  }

1.2 将获取到的平级数据转换成父子关系的数据

//将数据库查询出来的平级数据转换成父子关系的数据
  public List<TreeVo<Permission>> menu(Permission permission,PageBean pageBean) throws Exception {
    List<TreeVo<Permission>> lst = new ArrayList<TreeVo<Permission>>();
    List<Permission> list = this.list(permission, pageBean);
    for (Permission p : list) {
      TreeVo<Permission> vo = new TreeVo<>();
      vo.setId(p.getId()+"");
      vo.setText(p.getName());
      vo.setParentId(p.getPid()+"");
      lst.add(vo);
    }
    return BuildTree.buildList(lst, "-1");
  }

3.编写后台获取数据传输到前台

package com.YU.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.YU.dao.PermissionDao;
import com.YU.entity.Permission;
import com.YU.framework.ActionSupport;
import com.YU.framework.ModelDriver;
import com.YU.util.ResponseUtil;
import com.YU.util.TreeVo;
public class PermissionAction extends ActionSupport implements ModelDriver<Permission>{
  private Permission permission = new Permission();
  private PermissionDao pd = new PermissionDao();
  public void menus(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    List<TreeVo<Permission>> menus = pd.menu(null, null);
    ResponseUtil.writeJson(resp, menus);
  }
  @Override
  public Permission getModel() {
    return null;
  }
}

注:需要到XML文件中进行配置

<action path="/permission" type="com.zking.web.PermissionAction">
  </action>

4.前端代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%@include file="/common/header.jsp" %>
</head>
<body>
<div class="layui-layout layui-layout-admin">
  <div class="layui-header">
    <div class="layui-logo layui-hide-xs layui-bg-black">layout demo</div>
    <!-- 头部区域(可配合layui 已有的水平导航) -->
    <ul class="layui-nav layui-layout-left">
      <!-- 移动端显示 -->
      <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
        <i class="layui-icon layui-icon-spread-left"></i>
      </li>
      <!-- Top导航栏 -->
      <li class="layui-nav-item layui-hide-xs"><a href="">nav 1</a></li>
      <li class="layui-nav-item layui-hide-xs"><a href="">nav 2</a></li>
      <li class="layui-nav-item layui-hide-xs"><a href="">nav 3</a></li>
      <li class="layui-nav-item">
        <a href="javascript:;">nav groups</a>
        <dl class="layui-nav-child">
          <dd><a href="">menu 11</a></dd>
          <dd><a href="">menu 22</a></dd>
          <dd><a href="">menu 33</a></dd>
        </dl>
      </li>
    </ul>
    <!-- 个人头像及账号操作 -->
    <ul class="layui-nav layui-layout-right">
      <li class="layui-nav-item layui-hide layui-show-md-inline-block">
        <a href="javascript:;">
          <img src="//tva1.sinaimg.cn/crop.0.0.118.118.180/5db11ff4gw1e77d3nqrv8j203b03cweg.jpg" class="layui-nav-img">
          tester
        </a>
        <dl class="layui-nav-child">
          <dd><a href="">Your Profile</a></dd>
          <dd><a href="">Settings</a></dd>
        </dl>
      </li>
      <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
        <a href="javascript:;">
          <i class="layui-icon layui-icon-more-vertical"></i>
        </a>
      </li>
    </ul>
  </div>
  <div class="layui-side layui-bg-black">
    <div class="layui-side-scroll">
      <!-- 左侧导航区域(可配合layui已有的垂直导航) -->
      <ul id="menu" class="layui-nav layui-nav-tree" lay-filter="menu">
        <!-- <li class="layui-nav-item layui-nav-itemed">
          <a class="" href="javascript:;">menu group 1</a>
          <dl class="layui-nav-child">
            <dd><a href="javascript:;">menu 1</a></dd>
            <dd><a href="javascript:;">menu 2</a></dd>
            <dd><a href="javascript:;">menu 3</a></dd>
            <dd><a href="">the links</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item">
          <a href="javascript:;">menu group 2</a>
          <dl class="layui-nav-child">
            <dd><a href="javascript:;">list 1</a></dd>
            <dd><a href="javascript:;">list 2</a></dd>
            <dd><a href="">超链接</a></dd>
          </dl>
        </li>
        <li class="layui-nav-item"><a href="javascript:;">click menu item</a></li>
        <li class="layui-nav-item"><a href="">the links</a></li> -->
      </ul>
    </div>
  </div>
  <div class="layui-body">
    <!-- 内容主体区域 -->
    <div style="padding: 15px;">内容主体区域。记得修改 layui.css 和 js 的路径</div>
  </div>
  <div class="layui-footer">
    <!-- 底部固定区域 -->
            底部固定区域
  </div>
</div>
<script>
//JS 
layui.use(['element', 'layer', 'util'], function(){
  var element = layui.element
  ,layer = layui.layer
  ,util = layui.util
  ,$ = layui.$;
  $.ajax({
    url:"${pageContext.request.contextPath}/permission.action?methodName=menus",
    data:"json",
    success:function(data){
      console.log(data)
      var htmlStr = '';
      $.each(data, function(i, n) {
        htmlStr += '<li class="layui-nav-item layui-nav-itemed">';
        htmlStr += '<a class="" href="javascript:;">' + n.text + '</a>';
        if (n.hasChildren) {
          var children = n.children; 
          htmlStr += '<dl class="layui-nav-child">';
          $.each(children, function(idx, child) { // 使用 children 进行遍历
            htmlStr += '<dd><a href="javascript:;">' + child.text + '</a></dd>'; // 生成子节点的HTML字符串
          });
          htmlStr += '</dl>'; // 注意闭合 <dl> 标签位置
        }
        htmlStr += '</li>'; // 注意闭合 <li> 标签位置
      });
      $("#menu").html(htmlStr);
      element.render('menu');
    }
  })
});
</script>
</body>
</html>

5.实现思路

根据子级中的rid对应的父级id确定级别关系

例:

上图中的 id对应下图的表示的父级id

在遍历父级目录时,遍历子级目录寻找对应的id

目录
相关文章
|
数据库 数据安全/隐私保护
LayUI之树形菜单的实现
LayUI之树形菜单的实现
83 0
|
前端开发 Java 开发者
LayUI系列(二)之树形菜单的实现
LayUI系列(二)之树形菜单的实现
|
JSON Java 数据库
LayUI之动态树实现
LayUI之动态树实现
46 0
|
JavaScript
原生js树形菜单
原生js树形菜单
48 0
|
JavaScript 前端开发
52EasyUI 树形菜单- 树形菜单添加节点
52EasyUI 树形菜单- 树形菜单添加节点
55 0
|
前端开发 JavaScript 数据安全/隐私保护
Layui之动态树(树形菜单)详解1
Layui之动态树(树形菜单)详解1
373 0
|
前端开发 数据安全/隐私保护 UED
LayUI之树形菜单(权限管理)
LayUI之树形菜单(权限管理)
322 0
|
11月前
|
前端开发 数据格式
layui_02动态树
layui_02动态树
59 0
|
6月前
|
JSON 数据库 数据格式
Layui实现树状菜单
Layui实现树状菜单
46 0
|
11月前
|
前端开发 Java 数据库
Layui之动态树 左侧树形菜单栏 详细全面
Layui之动态树 左侧树形菜单栏 详细全面
457 0