Easyui(tree组件)

tree组件工具类的使用

实现tree效果
后台展示

理论:外层循环节点id如果等于内层循环节点的父id,那么久意味着内层循环当前的循环的节点是外层循环节点的儿子,
那么此时就可以将内层循环的节点放入外层循环绝点的children属性中;

基本包跟类
Easyui(tree组件)_第1张图片

实体类

package com.zhoujun.entity;

public class Menu {
	private String serialNo;
	private String menuid;
	private String menuname;
	private String menuURL;
	private String parentid;
	public String getSerialNo() {
		return serialNo;
	}
	public void setSerialNo(String serialNo) {
		this.serialNo = serialNo;
	}
	public String getMenuid() {
		return menuid;
	}
	public void setMenuid(String menuid) {
		this.menuid = menuid;
	}
	public String getMenuname() {
		return menuname;
	}
	public void setMenuname(String menuname) {
		this.menuname = menuname;
	}
	public String getMenuURL() {
		return menuURL;
	}
	public void setMenuURL(String menuURL) {
		this.menuURL = menuURL;
	}
	public String getParentid() {
		return parentid;
	}
	public void setParentid(String parentid) {
		this.parentid = parentid;
	}
	@Override
	public String toString() {
		return "Menu [serialNo=" + serialNo + ", menuid=" + menuid + ", menuname=" + menuname + ", menuURL=" + menuURL
				+ ", parentid=" + parentid + "]";
	}
	
	
	
	
}

dao类

package com.zhoujun.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhoujun.entity.Menu;
import com.zhoujun.entity.Permission;
import com.zhoujun.util.BaseDao;
import com.zhoujun.util.BuildTree;
import com.zhoujun.util.PageBean;
import com.zhoujun.vo.TreeVo;


public class MenuDao extends BaseDao<Menu>{
	
	public List<Menu> list(Menu menu,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql= "select * from t_easyui_menu";
		return super.executeQuery(sql, Menu.class, pageBean);	
	}
	 public List<TreeVo<Menu>> topNode(Menu menu,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
			List<Menu> list=this.list(menu, pageBean);
			List<TreeVo<Menu>> nodes=new ArrayList<TreeVo<Menu>>();
			TreeVo treeVo=null;
			for (Menu p : list) {
			  treeVo=new TreeVo<>();
			  treeVo.setId(p.getMenuid()+"");
			  treeVo.setText(p.getMenuname());
			  treeVo.setParentId(p.getParentid()+"");
			  Map<String, Object> attributes=new HashMap<String, Object>();
			  attributes.put("self", p);
			  treeVo.setAttributes(attributes);
			  nodes.add(treeVo);
			}
			return BuildTree.buildList(nodes,"-1");
			
			
		  }
	
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, SQLException {
		MenuDao menDao=new MenuDao();
		List<Menu> list = menDao.list(null, null);
		for (Menu m : list) {
			System.out.println(m);
		}
	}
}

助手类

package com.zhoujun.util;




import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhoujun.vo.TreeVo;

public class BuildTree {

	/**
	 * 默认-1为顶级节点
	 * @param nodes
	 * @param 
	 * @return
	 */
	public static <T> TreeVo<T> build(List<TreeVo<T>> nodes) {

		if (nodes == null) {
			return null;
		}
		//存放顶级节点(二级节点)的容器
		List<TreeVo<T>> topNodes = new ArrayList<TreeVo<T>>();

		for (TreeVo<T> children : nodes) {
			String pid = children.getParentId();
			if (pid == null || "-1".equals(pid)) {
				topNodes.add(children);
//如果是顶级节点,那么结束当前循环,继续下一次循环
				continue;
			}

			for (TreeVo<T> parent : nodes) {
				String id = parent.getId();
				if (id != null && id.equals(pid)) {
					parent.getChildren().add(children);
					children.setHasParent(true);
					parent.setChildren(true);
					continue;
				}
			}

		}
// 考虑到    数据库表中没有顶级节点(一级节点)
		TreeVo<T> root = new TreeVo<T>();
		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<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			root.setState(state);
		}

		return root;
	}

	/**
	 * 指定idparam为顶级节点
	 * @param nodes
	 * @param idParam
	 * @param 
	 * @return
	 */
	public static <T> List<TreeVo<T>> buildList(List<TreeVo<T>> nodes, String idParam) {
		if (nodes == null) {
			return null;
		}
		List<TreeVo<T>> topNodes = new ArrayList<TreeVo<T>>();

		for (TreeVo<T> children : nodes) {

			String pid = children.getParentId();
			if (pid == null || idParam.equals(pid)) {
				topNodes.add(children);

				continue;
			}

			for (TreeVo<T> 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;
	}

}

MenuAction

package com.zhoujun.web;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhoujun.dao.MenuDao;
import com.zhoujun.entity.Menu;
import com.zhoujun.util.ResponseUtil;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriven;

public class MenuAction extends ActionSupport implements ModelDriven<Menu>{
		private Menu menu=new Menu();
		private MenuDao menuDao=new MenuDao();
		@Override
		public Menu getModel() {
			// TODO Auto-generated method stub
			return menu;
		}
		
		
		public String menuTree(HttpServletRequest req,HttpServletResponse resp) {
			try {
				ResponseUtil.writeJson(resp, this.menuDao.topNode(null, null));
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			//结果码的配置就是为了,在mvc.xml中寻找重定向还是转发
			
			return null;
			
		}
}


xml配置

		<action path="/menu" type="com.zhoujun.web.MenuAction">
	<!-- 	<forward name="index" path="/index.jsp" redirect="false" /> -->
	</action> 

jsp界面

<%@ 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">
<!--全局样式  -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/default/easyui.css">
<!-- 定义图标 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/icon.css">
<!-- 主键库源码的jsp文件-->
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/index.js"></script>
<title>登录后的主界面</title>
</head>
<input type="hideen" id="ctx" value="${pageContext.request.contextPath }">
<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height:60px;background:#B3DFDA;padding:10px">管理系统</div>
	<div data-options="region:'west',split:true,title:'West'" style="width:150px;padding:10px;">
	<ul id="tt"></ul>
	
	</div>
	<div data-options="region:'east',split:true,collapsed:true,title:'East'" style="width:100px;padding:10px;">east region</div>
	<div data-options="region:'south',border:false" style="height:50px;background:#A9FACD;padding:10px;">zhoujun</div>
	<div data-options="region:'center',title:'Center'"></div>
</body>

</html>

运行结果:

Easyui(tree组件)_第2张图片

总结:下次见

你可能感兴趣的:(Easyui(tree组件))