菜单树数据形成
String sql="select * from STS_MALFUNC_SOLUTION s start with s.step_id=? connect by prior s.step_id = s.step_dept_id order by s.step_type Asc";
这是查询当前节点( s.step_id=? )的所有子孙节点(包含当前节点),并按照升序排序。
//菜单树的数据结构 public class GoJsData { private Integer id; private String text; private Integer size; private String color; private String weight; private List<GoJsData> causes = new ArrayList<GoJsData>(); // 子节点集合 public GoJsData(Integer id, String text, Integer size, String color, String weight, List<GoJsData> causes) { super(); this.id = id; this.text = text; this.size = size; this.color = color; this.weight = weight; this.causes = causes; } public GoJsData() { super(); }
}
具体的操作,如下:
@Test public void content() throws IOException { // 1 . 查询数据 STEP_ID:当前节点ID ;STEP_DEPT_ID:父节点ID String sql="select * from STS_MALFUNC_SOLUTION s start with s.step_id=? connect by prior s.step_id = s.step_dept_id order by s.step_type Asc"; List<Map<String,Object>> list= baseDao.queryForList(sql, new Object[] {"11000"}); // 2 . 剥离 当前节点,创建根数据 GoJsData g=null ; for (Map<String, Object> map : list) { String pstepId = String.valueOf(map.get("STEP_DEPT_ID")); if(Integer.valueOf(pstepId)==1) { g = new GoJsData( Integer.valueOf( String.valueOf(map.get("STEP_ID")) ), String.valueOf(map.get("STEP_CONTENT")), 18, "red", "Bold", new ArrayList<GoJsData>()); list.remove(map); break; } } //3 . 递归形成最终树数据 dd(list,g); System.out.println(JSONObject.toJSONString(g)); } // 递归操作 public GoJsData dd(List<Map<String,Object>> list,GoJsData g) { for (Map<String, Object> map : list) { int pstepId = Integer.valueOf(String.valueOf(map.get("STEP_DEPT_ID"))); System.out.println(pstepId); if(g.getId() == pstepId) { GoJsData gj = new GoJsData( Integer.valueOf( String.valueOf(map.get("STEP_ID")) ), String.valueOf(map.get("STEP_CONTENT")), 14, "red", "Bold", new ArrayList<GoJsData>()); g.getCauses().add(gj); dd(list,gj); } } return g; }
后端查询所有数据,前端拼接树结构
function createMenu(menu,pre){ let str = ``; for( let obj of menu){ if(obj.UP_MENU_ID==pre.MENU_ID) { if(obj.IS_LEAF ==1){ str=str+`<li> <a href="#"> <i class="menu-icon glyphicon glyphicon-home"></i> <span class="menu-text"> ${obj.MENU_NAME} </span> </a> </li> `; }else{ str=str+`<li> <a href="#"> <i class="menu-icon fa fa-desktop"></i> <span class="menu-text"> ${obj.MENU_NAME} </span> <i class="menu-expand"></i> </a> <ul class="submenu"> ${ createMenu(menu,obj)}</ul> </li> `;
}
}
}
return str;
}
MENU_ID 与 UP_MENU_ID : 节点ID 与 此节点的上级节点ID
前端拼接树
function treeData(data,pre){ let trees = []; for(let obj of data){ if(obj.PID == pre.ID){ trees.push( { text:obj.NAME,nodes:treeData(data,obj) } ); } } return trees; }