easyui 后台菜单数据转前台Treejson

 

因为使用easyui的tree菜单时不能够直接使用后台数据转的json对象,要先转换成tree要求的对象格式再转换成json.

首先创建一个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 + "]";
	}

}

先从数据库取到数据,由于没有使用实体类所以使用map集合因为map集合转换的json对象和java对象转换的是一样的

/**
	 * 
	* @Title: findmenu
	* @Description: (查询数据库里的菜单项)
	* @param map 前台页面上的参数集合
	* @return 因为是树形菜单所以不用分页
	* @return List<Map<String,Object>>  返回数据库的菜单集合  不能直接用于展示
	 * @throws Exception 
	 */
	public   List<Map<String, Object>> findmenu(Map<String, String[]> map) throws Exception{
		StringBuilder sb=new StringBuilder();
		sb.append("SELECT * from t_easyui_menu where true");
		/**
		 * 如果获取到菜单id
		 * 就将这个id作为父id去查询
		 */
		String paramvalue = JsonUtils.getParamvalue(map, "Menuid");
		if(StringUtils.isBlank(paramvalue)) {
			sb.append(" and parentid=-1");//最大的一级菜单
		}else {
			sb.append(" and parentid in ("+paramvalue+")");
		}
/**
 * 这里使用的是通用方法
 */
		return super.executeQuery(null, sb.toString());
	}

通用方法的关键代码为

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<String, Object>();
					for (int i = 1; i <=columnCount; i++) {
						/**
						 * 获取到列的名字作为键的值
						 */
						String key = metaData.getColumnName(i);
						map.put(key, rs.getObject(key));//用列名做键取值保存到map集合
					}
				list.add(map);
				}
				return list;

 

 

然后就是两个方法的递归调用注意用当前id做为父id取查询,如果为空的话可能会造成无限递归栈内存溢出


	/**
	 * 
	* @Title: mapTreeNode
	* @Description: (传入单个数据库菜单map集合)
	* @param map  数据库菜单对象
	* @param treeNode   树形菜单节点
	* @throws Exception
	* @return void
	 */
	public  void      mapTreeNode(Map<String, Object> map,TreeNode treeNode) throws Exception {
		 treeNode.setId(map.get("Menuid").toString());
		 treeNode.setText(map.get("Menuname").toString());
		treeNode.setAttributes(map);
		//还要设置子节点
		   /**
	     * 菜单的id作为父id去查询
	     */
		    Map<String, String[]> parammap=new HashMap<>();
    		  parammap.put("Menuid",new String[] {map.get("Menuid").toString()});
	         List<Map<String, Object>>lsmap = findmenu(parammap);
	         /**
	          * 如果有子节点
	          * 就调用MaplisttoTreeNodeList方法
	          * 将子节点复制
	          */
	         if(lsmap.size()>0) {
	         List<TreeNode> maplisttoTreeNodeList = MaplisttoTreeNodeList(lsmap);
	         treeNode.setChildren(maplisttoTreeNodeList);
	         }
	}
	
	/**
	 * 
	* @Title: MaplisttoTreeNodeList
	* @Description: (将传入的list<map>集合转换为list<TreeNode>集合)
	* @param lsmap  传入的数据库对象集合
	* @return
	* @throws Exception
	* @return List<TreeNode>
	 */
	public   List<TreeNode>  MaplisttoTreeNodeList(List<Map<String, Object>> lsmap) throws Exception{
		TreeNode treeNode=null;
		List<TreeNode> ls=new ArrayList<>();
		for (Map<String, Object> map : lsmap) {
			treeNode=new TreeNode();
			mapTreeNode(map, treeNode);
			ls.add(treeNode);
		}
		return ls;
	}

 

将这些方法组合一下

	/**
	 * 
	* @Title: getTreeNode
	* @Description: (返回一个TreeNode的集合)
	* @param map
	* @return
	* @throws Exception
	* @return List<TreeNode>
	 */
public  List<TreeNode>  getTreeNode(Map<String, String[]> map) throws Exception {
	/**
	 * 第一次手动查到数据
	 */
	List<Map<String, Object>> findmenu = this.findmenu(map);
	List<TreeNode> maplisttoTreeNodeList = this.MaplisttoTreeNodeList(findmenu);
	return maplisttoTreeNodeList;
}

子控制器,配置一下子控制器 

private MenuDao md=new MenuDao();

public  ActionForward  MenuTreeList(HttpServletRequest request,HttpServletResponse response) {
	Map<String, String[]> parameterMap = request.getParameterMap();
	try {
		List<TreeNode> treeNode = md.getTreeNode(parameterMap);
        String str = JSON.toJSONString(treeNode);
		super.print(str, response);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}

 

然后在前台请求子控制器

$(function(){
	$('#MenuTree').tree({    
	    url:'menuaction.action?methodName=MenuTreeList&Menuid='+$("#Menuid").val() 
	    	,onClick: function(node){
	    		if($('#Menutab').tabs('exists',node.text)){
	    			$('#Menutab').tabs('select',node.text)
	    		}else{
	    		$('#Menutab').tabs('add',{    
	    		    title:node.text,    
	    		    content:'<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuUrl+'" width="99%" height="99%"></iframe>',    
	    		    closable:true,      
	    		});  }

	    	}
	}); 
	
})

 

效果为:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值