EasyUI Tree Structure

Source: Internet
Author: User

EasyUI Tree Structure

Tree structures are common and commonly used. They have been used for a long time, but they basically call Methods written by others. It seems that they have not been too clear, at the time of ITOO development, I finally studied it and found that it is not difficult to implement the tree structure, and the implementation method is not the only one, before that, everyone was very inclined to use zTree. I searched on the Internet and found that zTtree is a powerful "tree plug-in", but I think it is not very suitable for me, first, easyUI is used in our front-end framework. easyUI has its own tree structure control. It is not necessary to introduce a zTree; in addition, for me, I am more familiar with the use of background java code to control, so I decided to study the background generation tree structure, use the json data format to pass the data to the easyUI tree control.

First of all, the easyUI tree structure has requirements for json data, so you can easily generate a tree structure by splicing data in the background into the format required by easyUI.

First, let's look at the entities that require a tree structure:

/*************************************** * ********************************* Module: institution. java * Author: Chen Lina * Description: institutional entities * Date: december 2, 2014 20:49:05 ************************************** * *******************************/package com. tgb. itoo. basic. entity; import java. util. hashSet; import java. util. set; import javax. persistence. cascadeType; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. fetchType; import javax. persistence. joinColumn; import javax. persistence. manyToOne; import javax. persistence. onetovel; import javax. persistence. table; import org. apache. log4j. varia. fallbackErrorHandler; import org. codehaus. jackson. annotate. jsonIgnore;/*** organization Entity */@ Entity @ Table (name = "TB_Institution ") public class Institution extends EntityDelete {/*** Organization Code */@ Column (name = "InstitutionCode", length = 255) private String institutionCode; /*** organization name */@ Column (name = "InstitutionName", length = 255) private String institutionName; /***** school */@ Column (name = "schoolSystem", length = 255) private String schoolSystem;/***** tuition fee */@ Column (name = "money ", length = 255) private String money;/*** organization type Id */@ Column (name = "nodeLevelId", length = 30) private String nodeLevelId; /*** school level */@ ManyToOne (cascade = CascadeType. DETACH, fetch = FetchType. EAGER, targetEntity = SchoolLevel. class) @ JoinColumn (name = "levelId", nullable = true) private SchoolLevel schoolLevel;/*** auto-join parent node */@ ManyToOne (cascade = CascadeType. REFRESH, fetch = FetchType. EAGER, targetEntity = Institution. class) @ JoinColumn (name = "pId", nullable = true) private Institution parentInstitution;/*** get/set Method omitted */}

Method class for converting an object to a tree structure required by easyUI:

First, define an object class that is consistent with the json data required by easyUI:

Package itoo. basic. tool. json; import java. util. arrayList; import java. util. list;/*** easyUI Tree Structure Conversion entity * @ author Chen Lina * @ version 1.0.0, january 29, 2015 10:39:28 */public class TreeNode {/*** display node id */private String id;/*** display node name */private String text; /*** display the node icon */private String icon;/*** display the node's parent node */private String parentId; /*** display the subnode set of the node */private List
 
  
Children;/*** empty constructor */public TreeNode () {}/*** the construction parameter with parameters * @ param id indicates the node ID * @ param text indicates the node name * @ param icon indicates the node icon * @ param parentId indicates the node id. * @ param children: displays the child node of the node */public TreeNode (String id, string text, String icon, String parentId, List
  
   
Children) {super (); this. id = id; this. text = text; this. icon = icon; this. parentId = parentId; this. children = children ;} /*** omit the get/set Method * // *** Add the subnode Method * @ param node tree node entity */public void addChild (TreeNode node) {if (this. children = null) {children = new ArrayList
   
    
(); Children. add (node) ;}else {children. add (node );}}}
   
  
 


After the object class is defined, convert the data to be displayed in a tree structure to the object type of treenode:

Package itoo. basic. tool. json; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import com. tgb. itoo. basic. entity. institution;/*** tree structure method class * @ author Chen Lina * @ version 1.0.0, january 29, 2015 10:44:55 */public class TreeUntil {/*** Method for converting an organizational unit into a treenode * @ param institutions list set of organizations to be converted * @ return returns a list of treenode Structures set */public List fillTree (List
 
  
Institutions) {List
  
   
List = new ArrayList
   
    
(); // Define a tree-like object Map map = new HashMap
    
     
(); // Define a maptry {// assign the passed parameter to the organization entity, and traverse each attribute that assigns the Organization to the treenode (Institution institution: institutions) {TreeNode node = new TreeNode (); node. setId (institution. getId (); // idnode. setText (institution. getInstitutionName (); // assign a value to the name // determine whether the organization node has a parent node. if the parent node is not empty, assign the value to the parent node if (institution. getParentInstitution ()! = Null) {node. setParentId (institution. getParentInstitution (). getId ();} map. put (institution. getId (), node); // place the Organization's id and tree structure entity in the map} for (Institution institution: institutions) {if (institution. getParentInstitution ()! = Null) {// if the parent node of the Organization node is not empty but the parent node id is empty, add the id of the organization in the list // map; // if the parent node id is not empty, the pId is defined as the parent node id; // two tree structure entities are defined, and a child node entity cNodeif (institution is added to the parent tree structure pNode. getParentInstitution (). getId () = null) {list. add (TreeNode) map. get (institution. getId ();} else {String pId = institution. getParentInstitution (). getId (); TreeNode pNode = (TreeNode) map. get (pId); TreeNode cNode = (TreeNode) map. get (institution. getId (); pNode. addChild (cNode) ;}} else {list. add (TreeNode) map. get (institution. getId () ;}} catch (Exception e) {// TODO: handle has tione. printStackTrace ();} return list; // returns a list set of tree structures }}
    
   
  
 


In this way, most of the work is completed. You also need to convert the data to json in the controller and receive the data on the jsp page:

/*** Query the tree structure of all organizations * @ param response transmits data to the foreground * @ param request obtains the transmitted data from the foreground */@ RequestMapping ("/info/queryAll ") private void queryAllType (HttpServletResponse response, HttpServletRequest request) {try {// query information List of all organizations
 
  
Institutions = institutionBean. queryAllInstitutions (); treeUntil = new TreeUntil (); // converts the queried data to the listList list = treeUntil in the tree structure. fillTree (institutions); jacksonJsonUntil = new JacksonJsonUntil (); // converts the returned list to json data and returns it to jacksonJsonUntil on the jsp page. beanToJson (response, list);} catch (Exception e) {e. printStackTrace ();}}
 


In this case, you can directly call this controller in jsp:

             
 

The final result is:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.