One, the tree JSON format
[{"id": 1, "text": "Rights Management", "Iconcls": "Icon-feed", "checked": false, "attributes": {"url": "#123"}, "Children": [{"id": 2, "text": "User Management "," Iconcls ":" Icon-users "," checked ": false," attributes ": { "url": null}, "state": null}, { "id": 3, "text": "Role Management", "Iconcls": "Icon-users", "checked": false, "Attributes": {"url": null}, "Children": [{ "id": Ten, "Text": "Role Management 1", "Iconcls": "Icon-users", "Checked": false, "attributes": {"url": null }, ' state ': null}], "state": N Ull}, {"id": 4, "Text": "Menu Management", "icon Cls ":" Icon-users "," checked ": false," attributes ": {" url ":" #222 " }, "state": null, "Children": []//Note the state value setting here is critical, if the parent node, state= "open "|" Close ", if it is a leaf node, only null//Error otherwise, ①}]}, {" id ": 5," text ":" System Module ", "Iconcls": "Icon-feed", "checked": false, "attributes": {"url": null}, "STA TE ": null}, {" id ": 6," text ":" System Module 2 "," Iconcls ":" Icon-feed "," checked ": false, "Attributes": {"url": null}}, {"id": 7, "text": "System Module 3", "icon Cls ":" Icon-feed ", "Checked": false, "attributes": {"url": null}}, {"id": 8, "t Ext ":" System Module 4 "," Iconcls ":" Icon-feed "," checked ": false," attributes ": {" url ": null }}, {"id": 9, "text": "System Module 5", "Iconcls": "Icon-feed", "checked": false, "Attributes": {"url": null}}]
①
The correct figure is
Second, tree loading mode
1. html format
<ul id= "tt" class= "Easyui-tree" > <li> <span>Folder</span> <ul> <li> <span>sub Folder 1</span> <ul> <li> <span><a href= "#" >file 11</a></span> </li> <li> <span>file 12</span> </li> <li> <span>file 13</span> </li> </ul> </li> <li> <span>file 2</span> </li> <li> <span>file 3& lt;/span> </li> </ul> </li> <li> <span>file 21</span> </li> </ul>
2, loading JSON files, such as Tree_data.json, content as above
<ul id= "tt" ></ul>
$ (' #tt '). Tree ({
URL: ' Tree_data.json '
});
3. The JSON string value from the background database ① entity class ②tree JSON format corresponding entity class ③ entity class menu converted to TreeNode Format (tree JSON format), recursive call back to the tree menu
import java.util.ArrayList;
Import Java.util.hashmap;import java.util.list;import Java.util.map;import Sys.entity.menu;public class TreeNodeUtil {public static list<treenode> tree (List<treenode>nodes,integer ID) {//recursively converted to tree list<treenode> Treenodes=new arraylist<treenode> (); for (TreeNode treenode:nodes) {TreeNode node=new TreeNode (); Node.setid (Treenode.getid ()); Node.settext (Treenode.gettext ()); Node.setattributes (Treenode.getattributes ()); Node.seticoncls (Treenode.geticoncls ()); Node.setstate (Treenode.getstate ()); Node.setpid (Treenode.getpid ()); if (Id==treenode.getpid ()) {Node.setchildren (Tree (nodes, Node.getid ())); Treenodes.add (node); }} return treenodes;}
Convert to TreeNode node public static TreeNode Tonode (Menu menu) {TreeNode node=new TreeNode (); Node.setid (Menu.getid ()); Node.seticoncls (Menu.geticon ()); Node.setpid (Menu.getparentmenuid ()); System.out.println (Node.getpid ()); Node.settext (Menu.getmenuname ()); Node.setstate (Menu.getstate ()); Map<string,object>attributes=new hashmap<string,object> (); Attributes.put ("url", Menu.getUrl ()); Node.setattributes (attributes); return node;} public static List<treenode>tolistnode (List<menu>menus) {list<treenode>nodes=new ArrayList< Treenode> (); For (menu menu:menus) {Nodes.Add (Tonode (menu));} return nodes;}}
④ query out the required menu records, I here is the rights management system, a role for multiple menus, through the role ID query out the menu record, a user has multiple roles, so the menu of multiple roles may have intersection, where the use of RBAC white list permissions, to get the maximum menu permissions.
@Overridepublic list<menu> Getmenubyroleid (integer[] roleids) {list<menu>menus=new Arraylist<menu > (); for (Integer roleid:roleids) {List<menu>menus1=menumapper.selectbyroleid (Roleid), for (Menu Menu:menus1 ) {if (!menus.contains (menu)) {Menus.add (menu);}}} return menus;
}
⑤controller Layer Code
@ResponseBody @RequestMapping (value= "treeview") Public list<treenode> TreeView (@RequestParam (" Roleids ") string roleidstr) { string Param[]=roleidstr.split (", "); Integer[] Roleids=new integer[param.length]; for (int i=0;i<param.length;i++) { roleids[i]=integer.parseint (param[i]); } List<menu>menus=menuservice.getmenubyroleid (roleids); List<treenode>nodes=treenodeutil.tolistnode (menus); List<treenode>treenodes=treenodeutil.tree (nodes, 0); for (TreeNode node:nodes) { System.out.println (node.geticoncls ()); System.out.println (Node.getpid ()); } return treenodes; }
Results:
⑥ Database Part Design
Easyui Tree Background JSON processing problem