《easyUI的树形菜单》

1:dao方法

package com.zy.dao;

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

import com.zy.entity.TreeNode;
import com.zy.util.JsonBaseDao;
import com.zy.util.JsonUtil;
import com.zy.util.PageBean;
import com.zy.util.StringUtils;

public class MenuDao extends JsonBaseDao{
	/**
	 * 查询后台需要展示的树形菜单的数据
	 * 注意:该数据转换成json对象,是不符合easyUI的tree组件展现的json格式
	 * @param paramMap
	 * @param pageBean
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> menuList(Map<String, String[]> paramMap,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String menuId = JsonUtil.getparamVal(paramMap, "Menuid"); //Menuid字段必须与数据库字段一致
		System.out.println(menuId);
		String sql=" select * from t_easyui_menu where true ";
//		判断id是否为空,再拼接SQL语句
		if(StringUtils.isNotBlank(menuId)) {
			sql+=" and parentid="+menuId;
		}else {
			sql+=" and parentid=-1 ";
		}
		return super.executeQuery(sql, pageBean);
	}
	
	/**
	 * 直接查询出来的数据不能展示,需转换成可以展示的数据
	 * @param map
	 * @param treeNode
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
    private void mapToTreeNode(Map<String, Object> map,TreeNode treeNode) throws InstantiationException, IllegalAccessException, SQLException {
    	//将查询出来的节点结果集设置给treeNode
    	treeNode.setId(map.get("Menuid").toString());  //1,id
    	treeNode.setText(map.get("Menuname").toString());  //2,展示内容
    	treeNode.setAttributes(map);  //3,描述父子节点,用于递归子节点
    	
//    	treeNode.setChildren(children);  4,树形菜单的节点,除了id以及展示文本,可能还伴有跳转页面,或者展示图片,等等一系列的描述都放到属性的map集合中
    	Map<String, String[]> paramMap=new HashMap<>();
    	//根据当前id作为父节点id,查询出他的所有的子节点
    	paramMap.put("Menuid", new String[] {treeNode.getId()});  //拿到当前id设置给menuList方法的参数paramMap,再次查询
    	List<Map<String, Object>> menuList = this.menuList(paramMap, null);//根据当前id再查询到他所有的子节点
    	List<TreeNode> TreeNodeList=new ArrayList<>();
    	mapListToTreeNodeList(menuList, TreeNodeList);
    	treeNode.setChildren(TreeNodeList);  //4,树形菜单的节点,除了id以及展示文本,可能还伴有跳转页面,或者展示图片,等等一系列的描述都放到属性的map集合中
    }
    
    /**
     * 遍历查询出的节点的方法
     * @param map
     * @param treeNode
     * @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, null); //根据页面传过来id查询节点
    	List<TreeNode> TreeNodeList=new ArrayList<>();
    	mapListToTreeNodeList(menuList, TreeNodeList);
		return TreeNodeList;
    }
    

}

2:easyUI的js代码

$(function() {
	
	var Aurl=$("#Aurl").val();
	
	$('#menuTree').tree({    
	    url:Aurl+'/menuAction.action?methodName=menuTreeList',  //请求全路径
	    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:false
	    	});
	    	
	      }
	    	
		}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值