Build a tree Menu
: supports multi-level menus.
Menu entity class:
Public classMenu {//Menu ID PrivateString ID; //Menu Name PrivateString name; //parent Menu ID PrivateString ParentID; //Menu URL PrivateString URL; //Menu Icon PrivateString icon; //Menu Order Private intorder; //Sub-menu PrivateList<menu>children; // ... Eliminates getter and setter methods and ToString methods}
Menus generally need to be sorted, and we sort according to the menu's Order field:
/ ** Sort, sorted according to order*/ PublicComparator<menu>order () {Comparator<Menu> Comparator =NewComparator<menu>() {@Override Public intCompare (menu O1, menu O2) {if(O1.getorder ()! =O2.getorder ()) { returnO1.getorder ()-O2.getorder (); } return0; } }; returnComparator; }
Ways to build a tree:
PublicMap<string,object>Findtree () {Map<String,Object> data =NewHashmap<string,object>(); Try{//Query All MenusList<menu> Allmenu =Menudao.findtree (); //root nodeList<menu> Rootmenu =NewArraylist<menu>(); for(Menu nav:allmenu) {if(Nav.getparentid (). Equals ("0")) {//The parent node is 0, which is the root node. Rootmenu.add (NAV); } } /*Sort by order of the menu class*/Collections.sort (Rootmenu, Order ()); //to set a submenu for the root menu, Getclild is called recursively. for(Menu nav:rootmenu) {/*Get all child nodes under the root node using the Getchild method*/List<Menu> childlist =Getchild (Nav.getid (), allmenu); Nav.setchildren (childlist);//set child nodes to the root node } /*** Output The built-in menu data. * */Data.put ("Success", "true"); Data.put ("List", Rootmenu); returndata; } Catch(Exception e) {data.put ("Success", "false"); Data.put ("List",NewArrayList ()); returndata; } }
Get submenu:
/*** Get child nodes *@paramID parent Node ID *@paramallmenu all Menu list *@returnAll sub-menu lists under each root node*/ PublicList<menu> Getchild (String id,list<menu>Allmenu) { //Sub-menuList<menu> childlist =NewArraylist<menu>(); for(Menu nav:allmenu) {//traverse all nodes to compare the parent ID of all menus to the ID of the root node passed in .//Equality Description: The child node of the root node. if(NAV. ParentID (). Equals (ID)) {childlist.add (NAV); } } //Recursive for(Menu nav:childlist) {Nav.setchildren (Getchild (Nav.getid (), allmenu)); } collections.sort (Childlist,order ());//Sort//returns an empty list if there are no child nodes under the node (recursive exit) if(childlist.size () = = 0){ return NewArraylist<menu>(); } returnchildlist; }
The last JSON string returned is as follows:
{ "Success": "True", "List": [ { "id": "1", "Name": "Java", "ParentID": "0", "url": "Http://www.aliouchen.com", "Order": 1, "Children": [ { "id": "2", "Name": "Concurrent Programming", "ParentID": "1", "url": "Http://www.aliouchen.com", "Order": 1, "Children": [] }, { "id": "3", "Name": "Multithreading", "ParentID": "1", "url": "Http://www.aliouchen.com", "Order": 2, "Children": [ "id": "4", "Name": "Thread", "ParentID": "3", "url": "Http://www.aliouchen.com", "Order": 1,
"Children": []}]}, {"id": "5", "Name": "Python", "ParentID": "0", "url": "Http://www.aliouchen.com", "Order": 2, "Children": [] } ]}
Java Build Tree Menu