easyUI树查询TreeGrid插件,connect by prior查询。

1.效果如图:


2.jsp页面:test.jsp

<div id="invoiceToolbar" style="height:auto;padding:8px">
			  
			<table id="test" title=" 供电所树查询" class="easyui-treegrid" style="width:700px;height:300px"
			data-options="
				url: '<%=path%>/rest/oper/getOrganizationJunc2',
				method: 'get',
				rownumbers: false,
				idField: 'id',
				treeField: 'name',
				animate:'true',
				loadFilter: myLoadFilter
			">
		<thead>
			<tr>
				<th field="id" width="220">id</th>
				<th field="name" width="500" align="centre">供电所</th>
				
			</tr>
		</thead>
			</table>

	<script>
		function myLoadFilter(data,parentId){
			var tg = $(this);
			var opts = tg.treegrid('options');
			opts.onBeforeExpand = function(row){
				if (row.children1){
					tg.treegrid('append',{
						parent: row[opts.idField],
						data: row.children1
					});
					row.children1 = undefined;
					tg.treegrid('expand', row[opts.idField]);
				}
				return row.children1 == undefined;
			};
			return data;
		}
		function setData(data){
			var todo = [];
			for(var i=0; i < data.length; i++){
				todo.push(data[i]);
			}
			while(todo.length){
				var node = todo.shift();
				if (node.children){
					node.state = 'closed';
					node.children1 = node.children;
					node.children = undefined;
					todo = todo.concat(node.children1);
				}
			}
		}	
	</script>

3.Controller页面:OperController.java
/**
	 * 树测试
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getOrganizationJunc2")
	public @ResponseBody JSONObject getOrganizationJunc2(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> json2 = operServiceBizc.getOrganizationJunc2(request);
		Gson gson = new Gson();  
		 String json = gson.toJson(json2);  
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(json);
		try {
			json="["+json+"]";
			out.print(new String(json.getBytes(),"ISO-8859-1"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
4.业务层:OperServiceImplBizc.java

	//测试2  树
	//
	@Override
	public Map<String, Object> getOrganizationJunc2(HttpServletRequest request) {
		String ywOrgCode2 = request.getParameter("ywOrgCode");
		String page2 = request.getParameter("page") == null?"1":request.getParameter("page");//第几页
		int pag2 = Integer.parseInt(page2);
		String rows2 = request.getParameter("rows") == null?"50":request.getParameter("rows");//每页多少行
		int row2 = Integer.parseInt(rows2);
		int endRow2 = pag2*row2;
		int startRow2 = 1;
		if(pag2 != 1){
			startRow2 = (pag2-1)*row2+1;
		}

		StringBuffer buf2 = new StringBuffer();
		if(ywOrgCode2!= null && !"".equals(ywOrgCode2)){
			buf2.append(" AND YW_ORG_CODE LIKE '%"+ywOrgCode2.trim()+"%'");
		}

		int total2 = this.getOrganizationJuncCount(buf2.toString());
		
		
		String sql2="select * from city start with id=1 connect by prior id=parent_id";
		List<Map> cityList = hibernateDao.queryForListWithSql(sql2);
		System.out.println("cityList==="+cityList);
		List<City> list = new ArrayList<City>();
		City city = null;
		for (int i = 0 , j = cityList.size(); i < j; i++) {
			Map map2 = cityList.get(i);
			city = new City();
			city.setId(Integer.parseInt(map2.get("ID") == null?"-1":map2.get("ID").toString()));
			city.setName(map2.get("NAME") == null?"":map2.get("NAME").toString());
			city.setParent_id(Integer.parseInt((map2.get("PARENT_ID") == null?"":map2.get("PARENT_ID").toString())));
			list.add(city);
		}
		Map<String, Object> map = null;  
		for (int i = 0; i < list.size(); i++) {  
	        City role = list.get(i);  
	        if (role.getId()==1) {  
	            map = new HashMap<String, Object>();  
	            //
	            map.put("id", list.get(i).getId());         //id  
	            map.put("name", list.get(i).getName());     //角色名  
	            map.put("children", createTreeGridChildren(list, role.getId()));  
	            System.out.println("map==="+map.get("name"));
	        }  
		}
		return map;
	}
	

	

	private Object createTreeGridChildren(List<City> list, Integer fid) {
		List<Map<String, Object>> childList = new ArrayList<Map<String, Object>>();  
	    for (int j = 0; j < list.size(); j++) {  
	        Map<String, Object> map = null;  
	        City treeChild = (City) list.get(j);  
	        if (treeChild.getParent_id()==fid) {  
	            map = new HashMap<String, Object>();  
	            //
	            map.put("id", list.get(j).getId());  
	            map.put("name", list.get(j).getName());  
	            map.put("children", createTreeGridChildren(list, treeChild.getId()));  
	        }  
	          
	        if (map != null)  
	            childList.add(map);  
	    }  
	    return childList;  
	}


5.实体类:city.java
package com.sgcc.ebill.invoice.webservice.entity;

public class City {

	private Integer id;
	private String name;
	private Integer parent_id;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getParent_id() {
		return parent_id;
	}
	public void setParent_id(Integer parent_id) {
		this.parent_id = parent_id;
	}
}

6.数据库表:city



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值