/** * 多级树形结构 * * @param response * @return */ @RequestMapping(value = "/listTree", produces = "application/json", method = RequestMethod.POST) public @ResponseBody ResponseEntity<ResultEntity> listTree( HttpServletResponse response, HttpServletRequest request) { ResultEntity result = new ResultEntityLinkedHashMapImpl(); Map<String, Object> content = new LinkedHashMap<String, Object>(); String path = request.getContextPath(); String detail = "<td><i style=\"width: 100px;display: inline-block;\"></i>排序 : %s</td>"; String operation = "<td><a href='" + path + "/menu/%s/toTreeEdit'>编辑</a></td>"; String item = "<table style='display: inline-block;'><tr><td style='width: 215px;'> %s</td>" + operation + detail + "</tr></table>"; try { Map<String, Object> params = new HashMap<String, Object>(); params.put("level", 1); List<SysMenu> menus = sysMenuService.find(params); if (menus != null) { int size = menus.size(); for (int i = 0; i < size; i++) { Map<String, Object> parent = new HashMap<String, Object>(); SysMenu menu_1 = menus.get(i); String id = menu_1.getId(); String name = menu_1.getName(); parent.put("name", String.format(item, name, id, menu_1.getSorter())); params.clear(); params.put("id", id); List<SysMenu> childList = sysMenuService.find(params); if (childList != null && childList.size() > 0) { Map<String, Object> xs = new LinkedHashMap<String, Object>(); findChild(childList, xs, item); Map<String, Object> children = new HashMap<String, Object>(); children.put("children", xs); parent.put("type", "folder"); parent.put("additionalParameters", children); } else { parent.put("type", "item"); } content.put(menu_1.getId(), parent); } } result.setStatus(ResultEntity.KW_STATUS_SUCCESS); } catch (ServiceException e) { result.setStatus(ResultEntity.KW_STATUS_FAIL); logger.error(e); } result.setResult(content); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); response.setCharacterEncoding("UTF-8"); return new ResponseEntity<ResultEntity>(result, headers, HttpStatus.OK); }
/** * 递归 遍历 树的子节点 * * @param menus * @param sonsMap * @throws ServiceException */ public void findChild(List<SysMenu> menus, Map<String, Object> sonsMap, String item) throws ServiceException { Map<String, Object> params = new HashMap<String, Object>(); if (menus != null && menus.size() > 0) { int size = menus.size(); for (int i = 0; i < size; i++) { SysMenu menu = menus.get(i); Map<String, Object> son = new HashMap<String, Object>(); son.put("name", String.format(item, menu.getName(), menu.getId(), menu.getSorter())); params.clear(); params.put("id", menu.getId()); List<SysMenu> subs = sysMenuService.find(params); // 判断是否还有子节点 if (subs != null && subs.size() != 0) { son.put("type", "folder"); Map<String, Object> xs = new LinkedHashMap<String, Object>(); findChild(subs, xs, item); Map<String, Object> children = new HashMap<String, Object>(); children.put("children", xs); son.put("additionalParameters", children); sonsMap.put(String.valueOf(i) + "-" + menu.getId(), son); } else { son.put("type", "item"); sonsMap.put(String.valueOf(i) + "-" + menu.getId(), son); continue; } } } }
/** * 多级树形结构 * * @param response * @return */ @RequestMapping(value = "/comboTree", produces = "application/json", method = RequestMethod.POST) public @ResponseBody ResponseEntity<String> comboTree( HttpServletResponse response, HttpServletRequest request) { JSONArray list = new JSONArray(); JSONObject result = new JSONObject(); List<Map<String, Object>> content = new ArrayList<Map<String, Object>>(); try { Map<String, Object> params = new HashMap<String, Object>(); params.put("level", 1); List<SysMenu> menus = sysMenuService.find(params); if (menus != null) { int size = menus.size(); for (int i = 0; i < size; i++) { Map<String, Object> parent = new HashMap<String, Object>(); SysMenu menu_1 = menus.get(i); String id = menu_1.getId(); String name = menu_1.getName(); params.clear(); params.put("id", id); List<SysMenu> childList = sysMenuService.find(params); parent.put("id", id); parent.put("text", name); if (childList != null && childList.size() > 0) { List<Map<String, Object>> xs = new ArrayList<Map<String, Object>>(); findSubTree(childList, xs); parent.put("children", xs); } content.add(parent); } } } catch (ServiceException e) { logger.error(e); } result.put("id", ""); result.put("text", "请选择父级树"); result.put("iconCls", "icon-save"); result.put("children", content); list.put(result); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); response.setCharacterEncoding("UTF-8"); return new ResponseEntity<String>(list.toString(), headers, HttpStatus.OK); }
/** * 增加树节点 * * @param model * @return * @throws ServiceException */ @RequestMapping("/toAddTree") public String toAddTree(Model model) throws ServiceException { List<Map<String, Object>> parents = sysMenuService.find(null); model.addAttribute("parents", parents); return "sys/tree_add"; }
/** * 递归 遍历树子节点 * * @param menus * @param sonsMap * @throws ServiceException */ public void findSubTree(List<SysMenu> menus, List<Map<String, Object>> sonList) throws ServiceException { Map<String, Object> params = new HashMap<String, Object>(); if (menus != null && menus.size() > 0) { int size = menus.size(); for (int i = 0; i < size; i++) { SysMenu menu = menus.get(i); Map<String, Object> son = new HashMap<String, Object>(); params.clear(); params.put("id", menu.getId()); List<SysMenu> subs = sysMenuService.find(params); // 判断是否还有子节点 son.put("id", menu.getId()); son.put("text", menu.getName()); if (subs != null && subs.size() != 0) { List<Map<String, Object>> xs = new ArrayList<Map<String, Object>>(); findSubTree(subs, xs); son.put("children", xs); } sonList.add(son); } } }