EasyUI 树形

1.树形tree的使用 用后台来体现父子节点的关系

    然后天用map集合来体现 那么我们先建立一个实体类来体现它们的父子关系把

package com.damei.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * tree树形的展示的实体类
 * @author Administrator
 *
 */
public class TreeNode {

	
	private String id;
	
	private String text;
	
	private String iconCls;
	/*
	 * 描述父子节点,用于递归子节点
	 */
	private List<TreeNode> children = new ArrayList<>();
	/*
	 * 树形菜单得子节点,除了id以及展示文本,有可能还包含跳转页面,或者图片展示 等一系列得描述
	 */
	private Map<String, Object> attributes = new HashMap<>();
	
	/*
	 * 提供set get 方法
	 */
	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 List<TreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}
	public Map<String, Object> getAttributes() {
		return attributes;
	}
	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	public String getIconCls() {
		return iconCls;
	}
	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}
	
	
	
	@Override
	public String toString() {
		return "TreeNode [id=" + id + ", text=" + text + ", iconCls=" + iconCls + ", children=" + children
				+ ", attributes=" + attributes + "]";
	}
	
	
}

 之后我们就在dao 方法里面写一个 递归   在者之前我们要导入几个架包(切记:架包要放在项目的webContent下的web-inf中的lib下)

3

之后我们用map集合写一个通用的查询方法

public List<Map<String, Object>> foreach(ResultSet rs) throws InstantiationException, IllegalAccessException,
					SecurityException, IllegalArgumentException, SQLException {
				// TODO Auto-generated method stub
				/*
				 *创建一个实体类的实列
				 *给创建的实体类的属性赋值
				 *将赋了值得属性加到list集合里去
				 */
				List<Map<String,Object>> list = new ArrayList<>();
				//获取原数据
				ResultSetMetaData metaData = rs.getMetaData();
				//获取列
				int columnCount = metaData.getColumnCount();
				Map<String, Object> map = null;
				while(rs.next()) {
					map = new HashMap<>();
				for (int i = 1; i <= columnCount; i++) {
					/*
					 * 获取列与值
					 */
					map.put(metaData.getColumnName(i), rs.getObject(i));
				}
				list.add(map);
				}
				return list;
			}
		});
	}

 在之后我们在dao层写一个递归的方法 体现树形的父子关系

public class MenuDao extends JsonBaesDao{


	/**
	 * 查询后台需要属性展示的菜单表数据(所有表字段)
	 * 注意:该数据转换成json对象,是不符合easyui的tree组件展现的json格式
	 * @param paramMap
	 * @param pageBean
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<Map<String, Object>> menuList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
	    String sql = "select * from t_easyui_menu where true";
	    //当前节点的id
	    String menuId = JsonUtitls.getPareMap(paramMap,"Menuid");
	    if(StringUtils.isNotBlank(menuId)) {
	    	sql += " and parentid in ("+menuId+")";
	    }else {
	    	sql += " and parentid=-1";
	    }
		return super.ExecuteQuery(sql, null);
	}
	
	/**
	 * 直接查出来的数据源是不能直接用于展示的,需要转换成可展示数据
	 * @param map
	 * @param treeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void mapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
		//查出来的数据通过map集合赋值给treeNode实体类
		treeNode.setId(map.get("Menuid").toString());
		treeNode.setText(map.get("Menuname").toString());
		treeNode.setIconCls(map.get("iconcls").toString());
		treeNode.setAttributes(map);
		
		Map<String, String[]> paraMap = new HashMap<>();
		//把当前节点的id当作父id,查出所有的子节点
		paraMap.put("Menuid", new String[] {treeNode.getId()});
		List<Map<String, Object>> menuList = this.menuList(paraMap, null);
		List<TreeNode> treeNodeList = new ArrayList<>();
		mapListToTreeNodeList(menuList, treeNodeList);
		//设置子节点
		treeNode.setChildren(treeNodeList);
	}
	
	/**
	 * 
	 * @param list 用来展示的
	 * @param treeNodeList 数据库查出来的数据
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void mapListToTreeNodeList(List<Map<String, Object>> list,List<TreeNode> treeNodeList) throws InstantiationException, IllegalAccessException, SQLException {
		TreeNode treeNode = null;
		for (Map<String, Object> map : list) {
			treeNode = new TreeNode();
			mapToTreeNode(map, treeNode);
			treeNodeList.add(treeNode);
		}
	}

	/**
	 * 这个的方法的返回值才是符合easyui的tree组件所需要的json格式
	 * @param paramMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<TreeNode> menuTreeList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		List<Map<String, Object>> menuList = this.menuList(paramMap, pageBean);
		List<TreeNode> treeNodeList = new ArrayList<>();
		mapListToTreeNodeList(menuList, treeNodeList);
		return treeNodeList;	
	}

}

在action中调用dao层的查询方法来展示树形

/**
	 * 树形展示
	 * @param req
	 * @param resp
	 * @return
	 */
	public String menuTreeList(HttpServletRequest req,HttpServletResponse resp) {
		
		try {
			List<TreeNode> menuTreeNodeList = this.menuDao.menuTreeList(req.getParameterMap(), null);
			//将获取的值转换成json对象格式
			 ObjectMapper objectMapper = new ObjectMapper();
			
			ResponseUtitls.Writer(resp, objectMapper.writeValueAsString(menuTreeNodeList));
		} catch (IllegalArgumentException | IllegalAccessException | InstantiationException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "index";
		
	}

menuTreeNodeList便是在后台js调用的方法   js方法的体现

$(function(){            //程序入口
	//获取树形的id
	$('#menuTree').tree({
		//查询展示树形菜单的url方法
		url:'menuAction.action?methodName=menuTreeList&&Menuid='+$("#Menuid").val(),
		onClick: function(node){
			if(node.attributes.menuURL!=null){
				//查询这个tab页是否打开  如果打开了就不能再次打开此tab
			if($('#tt').tabs('exists',node.text)){
				$('#tt').tabs('select',node.text);
			}else{
				//没有就增加一个tab页
				$('#tt').tabs('add',{    
					title:node.text,  
					//拿去数据库的url
					content:'<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>',    
					closable:true
				});  
			}
			}

		}

	});
})

结果展示

4

希望有帮助

猜你喜欢

转载自blog.csdn.net/qq_42471842/article/details/83241432