(JSON)无限级动态树形菜单升华版

为什么说这个是升华版的呢,因为以前做的无限级动态树形菜单是单个的,孤立的,只要读取出来的,单纯的......说了那么多,还不如一张图来的实在。放图

一张不行,那就两张,接着放图

图片中的文字都是自己瞎掰的,不代表任何意义!

发现不同了吗?

有头名称来表明你这是属于哪个分组下的,虽然我以前也不知道这什么玩意儿,一个json就能写出来?郁闷了几天,终于在老大不催我进度的时候,弄出来了

前台可以使用layui的框架,里面有树形菜单的样式,(树形菜单的复选框功能等是今年六月才出来,是的,真的很巧的了,太好用,点赞)网址:https://www.layui.com/demo/tree.html

后台只写json代码:

public String nodestree(){
		JSONArray jsonArray = new JSONArray();
		Map<Object, Object> jsonMapList1 = new HashMap<Object, Object>();
		jsonMapList1.put("title", "自定义分组");
		jsonMapList1.put("id", "");
		 Pager pager = new Pager();
	       
	        pager.setPageSize(9000);
	        pager.setOrderBy("orderList");
	        pager.setOrderType(OrderType.asc);
	        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(RescueUnit.class); 
	        detachedCriteria.add(Restrictions.isNull("parent"));
	      
	        Pager findByPager =rescueUnitService.findByPager(pager, detachedCriteria);
		    
	        @SuppressWarnings({ "unchecked", "unused" })
			List<RescueUnit> getRescueUnitList = findByPager.getList();
	         
	        for(RescueUnit mg:getRescueUnitList){
	        	Map<Object,Object> jsonMapList=new HashMap<Object,Object>();
	        	jsonMapList.put("title", mg.getUnitname());
	        	jsonMapList.put("id", mg.getId());
	        	jsonMapList.put("pid", mg.getParent() == null ? "" : mg.getParent().getId());
	        	
	        	if(mg.getChildren() != null){
	        		getChildrenArray(mg, jsonMapList);
	        	}
	        jsonArray.add(jsonMapList);
	        }
	       
	        jsonMapList1.put("children", jsonArray);
	       
	        JSONArray jsonArray2 = new JSONArray();
	        jsonArray2.add(jsonMapList1);
	        
	        JSONArray jsonArray3 = new JSONArray();
	       
	        Map<Object, Object> jsonMapList2 = new HashMap<Object, Object>();
			jsonMapList2.put("title", "企业微信通讯录");
			jsonMapList2.put("id", "00");
			 Pager pager1 = new Pager();
		       
		        pager1.setPageSize(9000);
		        pager1.setOrderBy("orderList");
		        pager1.setOrderType(OrderType.asc);
		        DetachedCriteria detachedCriteria1 = DetachedCriteria.forClass(MemberGroup.class); 
		        detachedCriteria1.add(Restrictions.isNull("parent"));
		      
		        Pager findByPager1 =memberGroupService.findByPager(pager1, detachedCriteria1);
			    
		        @SuppressWarnings({ "unchecked", "unused" })
				List<MemberGroup> getMemberGroupList = findByPager1.getList();
		         
		        for(MemberGroup mg:getMemberGroupList){
		        	Map<Object,Object> jsonMapList3=new HashMap<Object,Object>();
		        	jsonMapList3.put("title", mg.getGroupName());
		        	jsonMapList3.put("id", mg.getId());
		        	jsonMapList3.put("pid", mg.getParent() == null ? "" : mg.getParent().getId());
		        	
		        	if(mg.getChildren() != null){
		        		getChildrenArray1(mg, jsonMapList3);
		        	}
		        	
		        	
		        	jsonArray3.add(jsonMapList3);
		        }
		        
		        jsonMapList2.put("children", jsonArray3);
		        jsonArray2.add(jsonMapList2);
	        
	       return  jsonArray2.toString();
	}
private void getChildrenArray(RescueUnit mg, Map<Object, Object> jsonMapList) {
		JSONArray childrenArray = new JSONArray();
		for(RescueUnit children : mg.getChildren()){
			Map<Object,Object> childrenMap = new HashMap<Object,Object>();
			childrenMap.put("title", children.getUnitname());
			childrenMap.put("id", children.getId());
			jsonMapList.put("pid", children.getParent() == null ? "" : children.getParent().getId());
			if(children.getChildren() != null){
        		getChildrenArray(children, childrenMap);
        	}
//			JSONArray jsonArray4 = new JSONArray();
//
// 			List<Staff> stafUnitAllList = staffService.getList("rescueUnit.id", children.getId());
// 			for (Staff mg1 : stafUnitAllList) {
// 				Map<Object, Object> jsonMapList4 = new HashMap<Object, Object>();
// 				jsonMapList4.put("title", mg1.getName());
// 				jsonMapList4.put("id", mg1.getId());
// 				jsonMapList4.put("pid",children.getId());
// 				jsonArray4.add(jsonMapList4);
// 			}
// 			childrenMap.put("children", jsonArray4);
			childrenArray.add(childrenMap);
		}
		
		jsonMapList.put("children", childrenArray);
	}
	private void getChildrenArray1(MemberGroup mg, Map<Object, Object> jsonMapList3) {
		JSONArray childrenArray = new JSONArray();
		for(MemberGroup children : mg.getChildren()){
			Map<Object,Object> childrenMap = new HashMap<Object,Object>();
			childrenMap.put("title", children.getGroupName());
			childrenMap.put("id", children.getId());
			jsonMapList3.put("pid", children.getParent() == null ? "" : children.getParent().getId());
			if(children.getChildren() != null){
        		getChildrenArray1(children, childrenMap);
        	}
			childrenArray.add(childrenMap);
		}
		jsonMapList3.put("children", childrenArray);
	}

json格式写来写去其实就是一样的,关键在于你是如何思考你要的json是什么样,无限级的下级用的也是递归的方式要加油!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值