Recently in the project, we need to iterate and show the Tree menu, here we are using BOOTSTRACP, so we are using the BOOTSTRACP multilevel list tree,
When used, it's very simple, just
<!--Required stylesheets--><link href= "./css/bootstrap.css" rel= "stylesheet" ><!--Required Javascript--><script src= "./js/jquery.js" ></script><script src= "./js/bootstrap-treeview.js" ></script>
As an example, you can show the tree on the page.
I'm not going to say this, ***************************************************.
Here, what I want to say is, some tricks in the background.
The database, when we consider the tree, is usually the ID, and ParentID.
Take the classification tree as an example, directly on the code:
< Select id="Loadchild" resultmap="Categoryresult"> Select *from WHERE = #id #</select>
/*** Convert categories to JSON data objects; * This can be returned directly, SPRINGMVC support to return JSON data; * Btree and Gson are used here to simplify the returned parameters. */@Override PublicString Deallisttojson (Category CG) {CG=Childnode (CG); BTree BT=NewBTree (); BT=Coverttotree (BT,CG); Gson Gson=NewGson (); returnGson.tojson (BT); }/*** Iteration Gets the child nodes of each level, fills the whole; * @paramCG *@return */ Privatecategory Childnode (category CG) {CG=Listchildnode (CG); if(Cg.getcategorylist (). Size () > 0) { for(Category cgTemp:cg.getCategoryList ()) {Childnode (cgtemp); } } returnCG; }PrivateBTree coverttotree (BTree bt,category CG) {Bt.settext (Cg.getname ()); Bt.setid (Cg.getid ()); List<BTree> btlist =NewArraylist<btree>(); if(Cg.getcategorylist (). Size () > 0) { for(Category cgTemp:cg.getCategoryList ()) {BTree bttemp=NewBTree (); Bttemp=Coverttotree (bttemp,cgtemp); Btlist.add (bttemp); }} bt.setnodes (Btlist); returnBT; }/*** Gets the next level child node of the current category, "Just get Next Level" *@paramCG *@return */ Publiccategory Listchildnode (category CG) {cg.setcategorylist (Getsqlmapclienttemplate (). queryForList ("Category.loadchild", CG)); returnCG; }
iteration, every time to request the database, where you can use the cache. Not yet added. [Actually we don't have a lot of nodes]
Here, using Gson to convert the list, in fact, as with the others,
Using a custom btree, this can reduce some unnecessary attributes to the tree's JSON string.
。。
The final data format returned:
{"Text": "Book Classification", "id": 1, "nodes": [{"Text": "Textile Professional book", "id": 9, "nodes": [{"Text": "Textile Manual book", "id": One, "nodes": []},{"text": " Textile Processing Books "," id ":" Nodes ": []}]},{" text ":" Textile Machinery book "," id ": Ten," Nodes ": []}]}
That's it,
A little:
var tree = [${gsoncol}]; $ (function() { // $ (' #tree '). TreeView ({ Data:tree }); // node is selected function (event, data) { $ ("#hidClo_parent"). Val (data.id); }); $ (' #tree_main '). TreeView ({ data:tree });
At initialization time, var tree=[${gsoncol}], use [] here, otherwise the tree will not be displayed.
Some memo information about the Java Display Tree menu. [Bootstrap Tree]