easyui服务器端对树的构造,即easyui tree响应数据格式

/**
	 * 获取用户的权限范围内的区域菜单树
	 * 
	 * @return
	 */
	public String terminalGroupTree() {
		// 得到当前用户
		String username = (String) SecurityUtils.getSubject().getPrincipal();
		User user = userService.findByUsername(username);
		List<TerminalMenu> menus = (List<TerminalMenu>) menuManageService
				.getAllTerminalMenus();
		// 获得用户权限范围类的区域权限和根权限
		List<TerminalMenu> userTerminalMenus = new ArrayList<>();
		List<TerminalMenu> topTerminalMenus = new ArrayList<>();
		for (TerminalMenu menu : menus) {

			userTerminalMenus.add((TerminalMenu) menu);
			if (null == menu.getPid()) {
				topTerminalMenus.add((TerminalMenu) menu);
			}
		}

		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("[{\"id\":\"-1\",\"text\":\"区域\",\"children\":");
		Collections.sort(topTerminalMenus);
		buildTreeJson(topTerminalMenus, stringBuffer, userTerminalMenus);
		stringBuffer.append("}]");
		// 直接返回原始字符串
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println(stringBuffer.toString());
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 生成菜单树JSON
	 * 
	 * @param menus
	 * @param stringBuffer
	 */
	private void buildTreeJson(List<TerminalMenu> menus, StringBuffer json,
			List<TerminalMenu> userTerminalMenus) {
		json.append("[");
		if (menus != null && menus.size() > 0) {
			for (int i = 0; i < menus.size(); i++) {
				TerminalMenu terminalMenu = menus.get(i);
				json.append("{");
				json.append("\"id\"");
				json.append(":");
				json.append(terminalMenu.getId());
				// ||MenuType.City.toString().equals(terminalMenu.getType())
				if (MenuType.Province.toString().equals(terminalMenu.getType())) {
					json.append(",");
					json.append("\"state\"");
					json.append(":");
					json.append("\"");
					json.append("closed");
					json.append("\"");
				}
				json.append(",");
				json.append("\"text\"");
				json.append(":");
				json.append("\"");
				json.append(terminalMenu.getName());
				json.append("\"");
				json.append(",");
				json.append("\"attributes\":");
				json.append("{\"refId\":");
				json.append(terminalMenu.getRefId());
				if (MenuType.Terminal.toString().equals(terminalMenu.getType())) {
					Terminal terminal = terminalService
							.getTerminal(terminalMenu.getRefId());
					if (null != terminal) {
						json.append(",\"online\":");
						json.append(terminal.isOnline());
						json.append(",\"boxID\":\"");
						json.append(terminal.getBoxId());
						json.append("\"");
					}
				}

				json.append(",\"type\":\"");
				json.append(terminalMenu.getType());
				json.append("\"}");
				List<TerminalMenu> children = new ArrayList<TerminalMenu>();
				children.addAll(findTerminalMenusFromListByPid(
						terminalMenu.getId(), userTerminalMenus));
				Collections.sort(children);
				if (children != null && children.size() > 0) {
					json.append(",");
					json.append("\"children\"");
					json.append(":");
					buildTreeJson(children, json, userTerminalMenus);
				}
				json.append("}");
				if (i != menus.size() - 1) {
					json.append(",");
				}
			}
		}
		json.append("]");
	}

	/**
	 * 在menus中找符合pid的menus
	 * 
	 * @param pid
	 * @param menus
	 * @return
	 */
	public List<TerminalMenu> findTerminalMenusFromListByPid(Long pid,
			List<TerminalMenu> menus) {
		List<TerminalMenu> list = new ArrayList<>();
		if (null != menus) {
			for (TerminalMenu terminalMenu : menus) {
				if (null != terminalMenu.getPid()
						&& pid.longValue() == terminalMenu.getPid().longValue()) {
					list.add(terminalMenu);
				} else {
					continue;
				}
			}
		}
		return list;
	}
通过以上方法返回的数据格式如下
[
    {
        "id": "-1",
        "text": "区域",
        "children": [
            {
                "id": 118,
                "state": "closed",
                "text": "客运",
                "attributes": {
                    "refId": 117,
                    "type": "Province"
                },
                "children": [
                    {
                        "id": 124,
                        "text": "大巴",
                        "attributes": {
                            "refId": 123,
                            "type": "City"
                        },
                        "children": [
                            {
                                "id": 159,
                                "text": "351564060956810",
                                "attributes": {
                                    "refId": 158,
                                    "online": false,
                                    "boxID": "351564060956810",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 473,
                                "text": "352482034997118",
                                "attributes": {
                                    "refId": 472,
                                    "online": false,
                                    "boxID": "352482034997118",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 274,
                                "text": "352575071663913",
                                "attributes": {
                                    "refId": 273,
                                    "online": false,
                                    "boxID": "352575071663913",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 144,
                                "text": "352575073269313",
                                "attributes": {
                                    "refId": 143,
                                    "online": false,
                                    "boxID": "352575073269313",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 126,
                                "text": "55555555",
                                "attributes": {
                                    "refId": 125,
                                    "online": false,
                                    "boxID": "55555555",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 410,
                                "text": "86877000",
                                "attributes": {
                                    "refId": 409,
                                    "online": false,
                                    "boxID": "86877000",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 155,
                                "text": "868770001662336",
                                "attributes": {
                                    "refId": 154,
                                    "online": false,
                                    "boxID": "868770001662336",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 388,
                                "text": "868770001668168",
                                "attributes": {
                                    "refId": 387,
                                    "online": false,
                                    "boxID": "868770001668168",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 460,
                                "text": "868770001668200",
                                "attributes": {
                                    "refId": 459,
                                    "online": false,
                                    "boxID": "868770001668200",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 151,
                                "text": "868770001678167",
                                "attributes": {
                                    "refId": 150,
                                    "online": false,
                                    "boxID": "868770001678167",
                                    "type": "Terminal"
                                }
                            },
                            {
                                "id": 327,
                                "text": "868770001679363",
                                "attributes": {
                                    "refId": 326,
                                    "online": false,
                                    "boxID": "868770001679363",
                                    "type": "Terminal"
                                }
                            }
                        ]
                    },
                    {
                        "id": 120,
                        "text": "火车",
                        "attributes": {
                            "refId": 119,
                            "type": "City"
                        },
                        "children": [
                            {
                                "id": 134,
                                "text": "44444444",
                                "attributes": {
                                    "refId": 133,
                                    "online": false,
                                    "boxID": "44444444",
                                    "type": "Terminal"
                                }
                            }
                        ]
                    }
                ]
            },
            {
                "id": 112,
                "state": "closed",
                "text": "四川",
                "attributes": {
                    "refId": 111,
                    "type": "Province"
                },
                "children": [
                    {
                        "id": 128,
                        "text": "成都",
                        "attributes": {
                            "refId": 127,
                            "type": "City"
                        },
                        "children": [
                            {
                                "id": 130,
                                "text": "66666666",
                                "attributes": {
                                    "refId": 129,
                                    "online": false,
                                    "boxID": "66666666",
                                    "type": "Terminal"
                                }
                            }
                        ]
                    },
                    {
                        "id": 114,
                        "text": "绵阳",
                        "attributes": {
                            "refId": 113,
                            "type": "City"
                        },
                        "children": [
                            {
                                "id": 116,
                                "text": "33333333",
                                "attributes": {
                                    "refId": 115,
                                    "online": false,
                                    "boxID": "33333333",
                                    "type": "Terminal"
                                }
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
具体传输的格式以及信息可根据实际业务添加修改

猜你喜欢

转载自blog.csdn.net/shenyanwei/article/details/74315965
今日推荐