tree
   根节点
   
   父节点/子节点

   枝节点/叶节点 

1.tree的使用 用后台来体现父子关系

后天用map集合来体现 首先建立一个实体类来体现那样的父子关系

public class TreeNode {
	private String id;
	private String text;
	private Map<String, Object> attributes = new HashMap<>();
	private List<TreeNode> children = new ArrayList<>();

	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 Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	public List<TreeNode> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}

	public TreeNode(String id, String text, Map<String, Object> attributes, List<TreeNode> children) {
		super();
		this.id = id;
		this.text = text;
		this.attributes = attributes;
		this.children = children;
	}

	public TreeNode() {
		super();
	}

	@Override
	public String toString() {
		return "TreeNode [id=" + id + ", text=" + text + ", attributes=" + attributes + ", children=" + children + "]";
	}

 2.在dao方法体现一个递归

需要导入的所有包

easyUI tree的使用

首先写一个map的通用 的查询方法

easyUI tree的使用 3.在dao层写递归的方法 体现父子关系

	/**
	 * 根据每一个id查询
	 * @param paramet
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String,Object>> MeiuList(Map<String, String[]> paramet) throws InstantiationException, IllegalAccessException, SQLException{
		String id = JsonUtils.getParamet(paramet, "Menuid");
		String sql="select * from t_easyui_menu where true";
		if(StringUtils.isNotBlank(id)) {
			sql+=" and parentid in ("+id+")";
		}else {
			sql+=" and parentid=-1";
		}
		System.out.println(sql);
		return super.findall(sql, null);
	}
	/**
	 * 拿到对象展示页面(用递归)
	 * @param map
	 * @param menu
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public void maptoMenu(Map<String,Object> map,Menu menu) throws InstantiationException, IllegalAccessException, SQLException {
//	拿到每一个对象
		menu.setId(map.get("Menuid").toString());
		menu.setText(map.get("Menuname").toString());
		menu.setAttributes(map);
		
//		menu.setChildren(children);
		
		Map<String,String[]> maps=new HashMap<>();
//		拿到当前子节点id
		maps.put("Menuid", new String[] {menu.getId()});
//		再次查询
		List<Map<String, Object>> meiuList = this.MeiuList(maps);
		List<Menu> menus=new ArrayList<>();
//		递归查询有的子节点再次调用集合 把每一个字节赋值
		mapListTomenuList(meiuList, menus);
		menu.setChildren(menus);
	}
	
	/**
	 * 给集合里面的对象赋值
	 * @param list
	 * @param menus
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public void mapListTomenuList(List<Map<String,Object>> list,List<Menu> menus) throws InstantiationException, IllegalAccessException, SQLException {
		Menu menu=null;
//		遍历map集合 
		for (Map<String,Object> map : list) {
			menu=new Menu();
			maptoMenu(map, menu);
//			集合吧每一个menu放进去	
			menus.add(menu);
		}
	}
	/**
	 * 返回值 才是符合easyui的tree组件所需的json格式
	 * @param map
	 * @return
	 * @throws InstantiationException 
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Menu> lists(Map<String,String[]> map) throws InstantiationException, IllegalAccessException, SQLException{
		List<Map<String, Object>> meiuList = this.MeiuList(map);
		List<Menu> menus=new ArrayList<>();
		mapListTomenuList(meiuList, menus);
		return menus;
	}

4.lists便是在后台js调用的方法

easyUI tree的使用

5.js的体现

easyUI tree的使用

 

6.最后的结果

easyUI tree的使用

相关文章: