EasyUI中Tree的前端实现

前言

前面一篇使用一张表实现了数据后端的输出(点击这里),这篇我们使用另一张表实现前端的展示

实现效果及使用表

在这里插入图片描述

使用的表数据:

在这里插入图片描述

案例

Menudao: 把List 得到的单个Menu转换为treevo,在将treevo加入到nodes
package com.shegx.dao;

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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.shegx.entity.Menu;
import com.shegx.entity.Permission;
import com.shegx.util.BaseDao;
import com.shegx.util.BuildTree;
import com.shegx.util.PageBean;
import com.shegx.vo.TreeVo;

public class MenuDao extends BaseDao<Menu>{

	public List<Menu> list(Menu menu,PageBean pageBean) throws Exception {
		String sql="select * from t_easyui_menu where true";
		return super.executeQuery(sql, Menu.class, pageBean);
	}
	
	
	public List<TreeVo<Menu>> topNode(Menu menu,PageBean pageBean) throws Exception {
		
		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 Exception {
		MenuDao md = new MenuDao();
		List<Menu> list = md.list(null, null);
		
//		通锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街革拷锟斤拷锟绞斤拷锟斤拷锟斤拷
		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);
		}
		
		TreeVo<Menu> parent = BuildTree.build(nodes);
		ObjectMapper om = new ObjectMapper();
		String jsonstr = om.writeValueAsString(parent);
		System.out.println(jsonstr);
		
		
		
	}
	
}

工具类:ResponseUtil
把传过来的对象转换为json格式
package com.shegx.util;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;

public class ResponseUtil {

	public static void write(HttpServletResponse response,Object o)throws Exception{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(o.toString());
		out.flush();
		out.close();
	}
	
	public static void writeJson(HttpServletResponse response,Object o)throws Exception{
		response.setContentType("text/html;charset=utf-8");
		ObjectMapper om = new ObjectMapper();
		String jsonstr = om.writeValueAsString(o);
		PrintWriter out=response.getWriter();
		out.println(jsonstr.toString());
		out.flush();
		out.close();
	}
}

MenuAction
package com.shegx.web;

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

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

public class MenuAction extends ActionSupport implements ModelDriven<Menu> {

	private  Menu menu = new Menu();
	private  MenuDao md =   new MenuDao();

	@Override
	public Menu getModel() {
		// TODO Auto-generated method stub
		return menu;
	}
	
	
	public String menuTree(HttpServletRequest req,HttpServletResponse resp) {
		try {
//			TreeVo<Permission> topNode = this.pd.topNode(null, null);
//			List<TreeVo<Permission>> list = new ArrayList<TreeVo<Permission>>();
//			list.add(topNode);
			ResponseUtil.writeJson(resp, this.md.topNode(null, null));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}

mvc.xml配置文件
<action path="/menu" type="com.shegx.web.MenuAction">

	</action>
js跳转
$(function(){
	$('#tt').tree({    
	    url:$("#ctx").val()+'/menu.action?methodName=menuTree'   
	});  
})
jsp页面
<%@ 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">
<!-- 全局样式 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/black/easyui.css">   
<!-- 定义图标 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/themes/black/easyui.css">   
<script type="text/javascript" src="${pageContext.request.contextPath }/static/js/jquery-easyui-1.5.1/jquery.min.js"></script>   
<!-- 主键库源码的JS文件 -->
<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="hidden" id="ctx" value="${pageContext.request.contextPath }">
<body class="easyui-layout">
	<div data-options="region:'north',border:false"
	 style="height:60px;background:#B3DFDA;padding:10px">xxx管理系统
	 </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;">版权所有
	 </div>
	<div data-options="region:'center',title:'Center'"></div>
	
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
easyui是一个基于jQuery的UI框架,提供了丰富的界面组件和交互效果,方便开发人员快速构建Web页面。而treeeasyui的一个树状组件,用于展示层级关系的数据。 在使用easyui tree进行java开发时,我们首先需要引入easyui的相关依赖包,并在页面引入相应的脚本和样式文件。然后,我们可以通过在页面定义一个div容器,将tree组件渲染在页面上。 在java后台代码,我们需要提供数据给tree组件进行展示。一般来说,我们可以通过数据库查询、接口调用等方式获取数据,并将数据转换为json格式。然后,将json数据返回给前端页面,供tree组件使用。 接下来,我们需要在前端页面初始化并配置tree组件。通过调用easyui提供的API,我们可以设置tree的数据源、展开图标、折叠图标、节点点击事件等。可以根据具体需求对tree进行自定义配置,以满足我们的业务需求。 在页面渲染完成后,easyui tree组件会自动将数据渲染为树状结构,并提供相应的交互功能,比如展开收起节点、选节点等。我们可以通过对tree组件的事件进行监听,实现特定操作,比如点击节点后加载子节点、在节点上右键弹出菜单等。 总之,通过使用easyui tree组件,结合java后台开发,我们可以方便地实现树形结构的展示和交互操作,提升用户体验,简化开发流程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值