layui_02动态树

简介: layui_02动态树
  • 管理界面树形菜单的步骤思路
  • 管理界面的树形菜单

管理界面树形菜单的步骤思路

1.学习layui的菜单模块

      1.1.分析前端需要什么样的数据?

      1.2.现在能够拿到什么样的数据?

      1.3.如何将现有的转换成需要的数据格式?

2.将对应的菜单数据构建出来

3.完成功能

管理界面的树形菜单

package com.zking.dao;
import java.util.ArrayList;
import java.util.List;
import com.zking.entity.Permission;
import com.zking.util.BaseDao;
import com.zking.util.BuildTree;
import com.zking.util.PageBean;
import com.zking.util.TreeVo;
public class PermissionDao extends BaseDao<Permission>{
public List<Permission> list(Permission permission, PageBean pageBean) throws Exception {
  String sql="select * from t_oa_permission";  
  return super.executeQuery(sql, Permission.class, pageBean);
}
//将数据库查询出的平级数据,转换成有父子关系的数据
public List<TreeVo<Permission>> menus(Permission permission, PageBean pageBean) throws Exception{
  List<TreeVo<Permission>> lst=new ArrayList<>();
  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");
}
}
package com.zking.dao;
import java.util.List;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.entity.Permission;
import com.zking.util.TreeVo;
public class PermissionDaoTest {
private  PermissionDao pd=new PermissionDao();
  @Test
  public void testList() throws Exception {
    List<Permission> list = pd.list(null, null);
    ObjectMapper om =new ObjectMapper();
    System.out.println(om.writeValueAsString(list));
  }
  @Test
  public void menus() throws Exception {
    List<TreeVo<Permission>> menus = pd.menus(null, null);
    ObjectMapper om =new ObjectMapper();
    System.out.println(om.writeValueAsString(menus));
  }
}
package com.zking.entity;
public class Permission {
private long id;
private String name;
private String description;
private String url;
private long pid;
private int ismenu;//控制当前系统的权限是菜单还是按钮
private long displayno;//菜单排序字段
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;
}
@Override
public String toString() {
  return "Permission [id=" + id + ", name=" + name + ", description=" + description + ", url=" + url + ", pid=" + pid
      + ", ismenu=" + ismenu + ", 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;
}
public Permission() {
  super();
  // TODO Auto-generated constructor stub
}
}
package com.zking.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zking.dao.PermissionDao;
import com.zking.entity.Permission;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
import com.zking.util.TreeVo;
public class PermissionAction extends ActionSupport implements ModelDriver<Permission> {
private Permission permission=new Permission();
private PermissionDao permissionDao=new PermissionDao();
public void menus(HttpServletRequest req, HttpServletResponse resp) {
  try {
    List<TreeVo<Permission>> menus = permissionDao.menus(null, null);
    ResponseUtil.writeJson(resp, menus);
  } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  } 
  }
  @Override
  public Permission getModel() {
    return permission;
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<config>
  <!-- <action path="/blog" type="com.zking.web.BlogAction">
    <forward name="list" path="/blogList.jsp" redirect="false" />
    <forward name="toList" path="/blog.action?methodName=list"
      redirect="true" />
    <forward name="toEdit" path="/blogEdit.jsp" redirect="false" />
  </action> -->
    <action path="/user" type="com.zking.web.UserAction">
  </action>
  <action path="/permission" type="com.zking.web.PermissionAction">
  </action>
<!--  <action path="/meetingInfo" type="com.zking.web.MeetingInfoAction"> -->
<!--    <forward name="list" path="/meetingInfoList.jsp" redirect="false" /> -->
<!--    <forward name="toList" path="/meetingInfo.action?methodName=list" -->
<!--      redirect="true" /> -->
<!--    <forward name="toEdit" path="/meetingInfoEdit.jsp" redirect="false" /> -->
<!--  </action> -->
<!--  <action path="/solrBlog" type="com.test.web.SolrBlogAction"> -->
<!--    <forward name="list" path="/solrBlogList.jsp" redirect="false" /> -->
<!--    <forward name="toList" path="/solrBlog.action?methodName=list" -->
<!--      redirect="true" /> -->
<!--    <forward name="toEdit" path="/solrBlogEdit.jsp" redirect="false" /> -->
<!--  </action> -->
  <action path="/studentBlog" type="com.test.web.StudentBlogAction">
    <forward name="list" path="/studentBlogList.jsp" redirect="false" />
    <forward name="toList" path="/studentBlog.action?methodName=list" redirect="true" />
    <forward name="toEdit" path="/studentBlogEdit.jsp" redirect="false" />
  </action>
  <action path="/solrBlog" type="com.xiaoli.web.SolrBlogAction">
    <forward name="list" path="/solrBlogList.jsp" redirect="false" />
    <forward name="toList" path="/solrBlog.action?methodName=list" redirect="true" />
    <forward name="toEdit" path="/solrBlogEdit.jsp" redirect="false" />
  </action>
</config>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- 引入 layui.css -->
<link rel="stylesheet" href="${pageContext.request.contextPath }/static/js/layui/css/layui.css">
<!-- 引入 layui.js -->
<script src="${pageContext.request.contextPath }/static/js/layui/layui.js"></script>
<script src="${pageContext.request.contextPath }/static/js/layui/layui.all.js"></script>
</head>
<body>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="common/header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>后台主页</title>
</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>
          <dd><a href="login.jsp">Sign out</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",
        dataType:'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,node){
                htmlStr+='<dd><a href="javascript:;">'+node.text+'</a></dd>';
                })
                htmlStr+='</dl>';
            }
            htmlStr+='</li>' 
          });
          $("#menu").html(htmlStr);
          element.render('menu');
        }
  });
  });
</script>
</body>
</html>

目录
相关文章
|
6月前
|
JavaScript
layui二级联动
layui二级联动
|
JSON Java 数据库
LayUI之动态树实现
LayUI之动态树实现
46 0
|
数据库 数据安全/隐私保护
LayUI之树形菜单的实现
LayUI之树形菜单的实现
82 0
|
前端开发 Java 开发者
LayUI系列(二)之树形菜单的实现
LayUI系列(二)之树形菜单的实现
|
前端开发 JavaScript 数据安全/隐私保护
Layui之动态树(树形菜单)详解1
Layui之动态树(树形菜单)详解1
366 0
|
前端开发 数据安全/隐私保护 UED
LayUI之树形菜单(权限管理)
LayUI之树形菜单(权限管理)
319 0
|
11月前
|
开发框架 前端开发 开发者
Layui----动态树
Layui----动态树
56 0
|
前端开发 Java 数据库
LayUI之动态树
LayUI之动态树
49 0
|
12月前
|
前端开发 数据可视化 JavaScript
Layui实现树形菜单(超详细)
Layui实现树形菜单(超详细)
151 0
|
前端开发 Java 开发者
LayUI之树形菜单
LayUI之树形菜单
67 0