SpringMVC + EasyUI asynchronous tree

Source: Internet
Author: User

Asynchronous tree has been used before: Struct2 + JQuery Ztree. For the latest project, SpringMVC + EasyUI must be used to generate an asynchronous tree. After a while, we will report the solution as follows: first, use EasyUI Tree components, official website Demo provided asynchronous Tree sample is PHP version, reference is of little significance, URL is: http://www.jeasyui.com/demo/main/index.php? Plugin = Tree & theme = default & dir = ltr & pitem =. For more information, see. Next, we will start to analyze the business: business scenarios: As shown in the preceding figure, there are classes under the grades and grades of the school. By default, all schools are loaded. After clicking a single school, all the grades of the school are asynchronously loaded, click grade to load the class. Js/jsp file: [javascript] <ul id = "gcttree" data-options = "animate: true, lines: true "> </ul> [html] $ ('# gcttree '). tree ({// asynchronous tree url: basePath + '/school/list_school_gct, onBeforeExpand: function (node) {if (node) {// change the url // currentId: current node id currentType: current node type $ ('# gcttree '). tree ('options '). url = basePath + "/school/list_school_gct? CurrentId = "+ node. id + "currentType =" + node. attributes. type ;}}}); used here, the onBeforeExpand event of the Tree component (API: http://www.jeasyui.com/documentation/index.php) onBeforeExpand node Fires before node is expanded, return false to cancel this expand action. that is to say, this event is called before a node is expanded each time. This is in line with the above business analysis. Click the school to trigger the asynchronous generation of the grade tree, and click the grade to trigger the asynchronous tree to generate the class tree. Controller layer: [java]/*** obtains the list of schools, grades, and classes (asynchronous tree) ** @ param request * @ return json data */@ RequestMapping (value = "/list_school_gct") @ ResponseBody public List <Map <String, Object> listSchoolGCT (HttpServletRequest request) {List <Map <String, Object> list = null; try {// current node id String currentId = request. getParameter ("currentId"); // the current node type String currentType = request. getParameter ("currentType"); list = SchoolService. treeData (currentId, currentType);} catch (Exception e) {logger. error (e);} return list;} service/dao layer: [java]/*** get tree data (asynchronous tree used for tree Assembly) ** @ param currentId current node id * @ param currentType current node type school = school grade = grade class = class ** @ return school, grade, class tree */public List <Map <String, object> treeData (String currentId, String currentType) {List <Map <String, Object> list = new struct List <Map <String, Object >>(); Map <String, String> attr = null; // if the current node type is null, it is the first time to load the school json if (StringUtils. isEmpty (currentType) {// query all schools list = ischoolDao. treeData (); for (Map <String, Object> school: list) {attr = new HashMap <String, String> (); attr. put ("type", "school"); school. put ("attributes", attr); // note that the school node must be closed to trigger the onBeforeExpand event school. put ("state", "closed") ;}// if the current node type is school, It asynchronously loads grade j Son else if (StringUtils. equals ("school", currentType) {// query the class list = igradeDao. listData (currentId) based on the school id; if (! CollectionUtils. isEmpty (list) {for (Map <String, Object> grade: list) {attr = new HashMap <String, String> (); attr. put ("type", "grade"); grade. put ("attributes", attr); // note that the grade node must be closed to trigger the onBeforeExpand event grade. put ("state", "closed") ;}}// if the current node type is grade, the class json else if (StringUtils. equals ("grade", currentType) {// query the class list = iclassDao Based on the grade id. listData (currentId); if (! CollectionUtils. isEmpty (list) {for (Map <String, Object> classs: list) {attr = new HashMap <String, String> (); attr. put ("type", "class"); classs. put ("attributes", attr) ;}} return list;} the SQL statement of dao is simple, that is, the select where statement.

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.