【EasyUI學習-2】Easyui Tree的異步加載


作者:ssslinppp      


1. 摘要


easyui相關的介紹可以上其官網或者百度去搜索,這里不做介紹。
Easyui Tree的使用,官網或者easyui中文網站,也有相關介紹,但是官方提供的實例所使用的json是寫死的,不是后台實時讀取的。在實際的項目中,要顯示的tree數據,一般都是從數據庫中讀取,然后通過通過ajax或者其他技術將tree的json數據發送到前台,然后顯示。
本文將介紹easyui tree的異步加載,以及手動展開tree。

2. tree的相關介紹



上圖是一個tree,它對應json數據格式為:

   
   
   
           
  1. [{
  2. "id":1,
  3. "text":"My Documents",
  4. "children":[{
  5. "id":11,
  6. "text":"Photos",
  7. "state":"closed",
  8. "children":[{
  9. "id":111,
  10. "text":"Friend"
  11. },{
  12. "id":112,
  13. "text":"Wife"
  14. },{
  15. "id":113,
  16. "text":"Company"
  17. }]
  18. },{
  19. "id":12,
  20. "text":"Program Files",
  21. "children":[{
  22. "id":121,
  23. "text":"Intel"
  24. },{
  25. "id":122,
  26. "text":"Java",
  27. "attributes":{
  28. "p1":"Custom Attribute1",
  29. "p2":"Custom Attribute2"
  30. }
  31. },{
  32. "id":123,
  33. "text":"Microsoft Office"
  34. },{
  35. "id":124,
  36. "text":"Games",
  37. "checked":true
  38. }]
  39. },{
  40. "id":13,
  41. "text":"index.html"
  42. },{
  43. "id":14,
  44. "text":"about.html"
  45. },{
  46. "id":15,
  47. "text":"welcome.html"
  48. }]
  49. }]

從上面的json數據可以看出,tree的數據有固定格式,一般都包括下面幾個:
  • id: 唯一標示;
  • text: 顯示的文本;
  • children:子節點;
  • state:closed或open,表示節點是展開還是折疊;
  • attributes:屬性,這里可以自定義若干屬性;
等,還有其他一些屬性,這里沒有一一列舉。
如果tree的內容不變,可以采用靜態的方式顯示,這個在官網上有實例,不再詳述。
若果想通過異步的方式加載tree的json數據,則后台只需按照tree的數據格式生成相應的json,然后返回前台即可。下面將介紹異步加載tree數據。

3. 異步加載tree數據,並實現tree的折疊展開


3.1 功能說明:

上圖是整個tree的節點信息,這些數據都是從數據庫中讀取並顯示的。但是在實際的項目中,可能tree的節點(children)會很多,若是一次全部加載,可能會很耗時,通常我們都是先加載父節點信息,然后點擊“展開”,再加載子節點信息,如下圖所示:

上圖中,我們首次加載時,【節點1】和【節點3】的子節點沒有展開(加載),而【節點2】的子節點全部展開,當點擊【節點1】的展開按鈕時,再加載【節點1】的子節點,如下圖所示:


3.2 前台代碼

jsp界面:

   
   
   
           
  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme() + "://"
  6. + request.getServerName() + ":" + request.getServerPort()
  7. + path + "/";
  8. response.setHeader("Pragma", "no-cache");
  9. response.setHeader("Cache-Control", "no-cache");
  10. response.setDateHeader("Expires", 0);
  11. %>
  12. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  13. <html>
  14. <head>
  15. <title>測試系統</title>
  16. <script type="text/javascript">var basePath = "<%=basePath%>";</script>
  17. <link rel="stylesheet" type="text/css" href="<%=basePath%>js/easyui/themes/default/easyui.css">
  18. <link rel="stylesheet" type="text/css" href="<%=basePath%>js/easyui/themes/icon.css">
  19. <link rel="stylesheet" type="text/css" href="<%=basePath%>js/easyui/demo.css">
  20. <script type="text/javascript" src="<%=basePath%>js/easyui/jquery.min.js"></script>
  21. <script type="text/javascript" src="<%=basePath%>js/easyui/jquery.easyui.min.js"></script>
  22. <script type="text/javascript" src="<%=basePath%>js/mytreeTest.js"></script>
  23. </head>
  24. <body>
  25. <h2>easyui tree</h2>
  26. <div class="easyui-layout" style="width:1300px;height:550px;">
  27. <div data-options="region:'west',split:true,border:false" title="導航菜單" style="width:200px">
  28. <ul id="myTree" class="easyui-tree"></ul>
  29. </div>
  30. <div data-options="region:'center'">
  31. </div>
  32. </div>
  33. </body>
  34. </html>

mytreeTest.js 
 
   
   
   
           
  1. $(function() {
  2. $('#myTree').tree({
  3. // checkbox: true,
  4. animate : true,
  5. lines : true,
  6. url : basePath + "loadTreeJson.action", //默認會將節點的id傳遞到后台
  7. loadFilter : function(data) { //必須有這個函數,否則出不來,不知道為什么
  8. return data.treeJson;
  9. },
  10. onClick : function(node) {
  11. alert("自己添加的屬性: 【URL】"+node.attributes.url+", 【info】"+node.attributes.info);
  12. }
  13. });
  14. });

3.3 后台代碼

我們采用了spring mvc。
為了實現tree的json格式數據的返回,我們在后台定義了一個類: TreeNodeInfo.java

   
   
   
           
  1. package com.ll.domain;
  2. import java.util.List;
  3. import java.util.Map;
  4. public class TreeNodeInfo {
  5. private String id; //要顯示的子節點的ID
  6. private String text; //要顯示的子節點的 Text
  7. private String state;
  8. private String iconCls; //節點的圖標
  9. private String parentId; //父節點的ID
  10. private List<TreeNodeInfo> children; //孩子節點的List
  11. private boolean checked = false;
  12. // private Map<String, Object> attributes = new HashMap<String, Object>();
  13. private Map<String, Object> attributes;
  14. public TreeNodeInfo() {
  15. super();
  16. }
  17. public TreeNodeInfo(String id, String text, String state, String iconCls,
  18. String parentId, List<TreeNodeInfo> children, boolean checked,
  19. Map<String, Object> attributes) {
  20. super();
  21. this.id = id;
  22. this.text = text;
  23. this.state = state;
  24. this.iconCls = iconCls;
  25. this.parentId = parentId;
  26. this.children = children;
  27. this.checked = checked;
  28. this.attributes = attributes;
  29. }
  30. public String getId() {
  31. return id;
  32. }
  33. public void setId(String id) {
  34. this.id = id;
  35. }
  36. public String getText() {
  37. return text;
  38. }
  39. public void setText(String text) {
  40. this.text = text;
  41. }
  42. public String getState() {
  43. return state;
  44. }
  45. public void setState(String state) {
  46. this.state = state;
  47. }
  48. public String getIconCls() {
  49. return iconCls;
  50. }
  51. public void setIconCls(String iconCls) {
  52. this.iconCls = iconCls;
  53. }
  54. public String getParentId() {
  55. return parentId;
  56. }
  57. public void setParentId(String parentId) {
  58. this.parentId = parentId;
  59. }
  60. public List<TreeNodeInfo> getChildren() {
  61. return children;
  62. }
  63. public void setChildren(List<TreeNodeInfo> children) {
  64. this.children = children;
  65. }
  66. public boolean isChecked() {
  67. return checked;
  68. }
  69. public void setChecked(boolean checked) {
  70. this.checked = checked;
  71. }
  72. public Map<String, Object> getAttributes() {
  73. return attributes;
  74. }
  75. public void setAttributes(Map<String, Object> attributes) {
  76. this.attributes = attributes;
  77. }
  78. }

loadTreeJson.action 
 
   
   
   
           
  1. package com.ll.web;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import javax.servlet.http.HttpServletRequest;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.ui.ModelMap;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import com.ll.domain.TreeNodeInfo;
  12. import com.ll.domain.User;
  13. import com.ll.service.IUserService;
  14. @Controller
  15. public class LoginController {
  16. @Autowired
  17. private IUserService userService;
  18. @RequestMapping(value = "/index.action")
  19. public String loginPage() {
  20. // return "login";
  21. return "myEasyuiTree";
  22. }
  23. @RequestMapping(value = "/loadTreeJson.action")
  24. public String loadTreeJson(ModelMap mm, String id,String info) {
  25. List<TreeNodeInfo> treeList = new ArrayList<TreeNodeInfo>();
  26. if((id==null) || "".equals(id)){ //首次加載tree節點
  27. //模擬從數據庫讀數據,並將讀出的數據賦值給treelist
  28. for (int i = 0; i < 5; i++) {
  29. TreeNodeInfo e = new TreeNodeInfo();
  30. e.setId(i+"");
  31. e.setText("節點【"+i+"】的內容");
  32. Map<String, Object> attributes = new HashMap<String, Object>();
  33. attributes.put("url", "www.baidu.com");
  34. attributes.put("info", "可以設置許多屬性值,這是第"+i+"個節點");
  35. e.setAttributes(attributes);
  36. //模擬子節點的數量-第1個和第3個有子節點,默認closed;
  37. if ((i==1) || (i==3)) {
  38. // 節點狀態,'open' 或 'closed',默認是 'open'。
  39. // 當設置為 'closed'時,該節點有子節點,並且將從遠程站點加載它們
  40. e.setState("closed");
  41. }
  42. //第2個節點也有子節點,但是默認open
  43. if((i==2)){
  44. List<TreeNodeInfo> node2ChildrenList = new ArrayList<TreeNodeInfo>();
  45. for (int j = 22; j < 25; j++) {
  46. TreeNodeInfo e2 = new TreeNodeInfo();
  47. e2.setId(j + "");
  48. e2.setText("節點【" + j + "】的內容");
  49. Map<String, Object> attributes2 = new HashMap<String, Object>();
  50. attributes2.put("url", "www.baidu.com");
  51. attributes2.put("info", "這是子節點【" + j + "】");
  52. e2.setAttributes(attributes2);
  53. node2ChildrenList.add(e2);
  54. }
  55. e.setChildren(node2ChildrenList);
  56. }
  57. treeList.add(e);
  58. }
  59. }else{ //展開節點
  60. //判斷節點的id號
  61. if("1".equals(id)){ //有3個子節點
  62. for (int i = 10; i < 13; i++) {
  63. TreeNodeInfo e = new TreeNodeInfo();
  64. e.setId(i + "");
  65. e.setText("節點【" + i + "】的內容");
  66. Map<String, Object> attributes = new HashMap<String, Object>();
  67. attributes.put("url", "www.baidu.com");
  68. attributes.put("info", "這是子節點【" + i + "】");
  69. e.setAttributes(attributes);
  70. treeList.add(e);
  71. }
  72. }else if ("3".equals(id)) { //有4個子節點
  73. for (int i = 30; i < 34; i++) {
  74. TreeNodeInfo e = new TreeNodeInfo();
  75. e.setId(i + "");
  76. e.setText("節點【" + i + "】的內容");
  77. Map<String, Object> attributes = new HashMap<String, Object>();
  78. attributes.put("url", "www.baidu.com");
  79. attributes.put("info", "這是子節點【" + i + "】");
  80. e.setAttributes(attributes);
  81. treeList.add(e);
  82. }
  83. }
  84. }
  85. mm.addAttribute("treeJson", treeList);
  86. return "treeJsonBean";
  87. }
  88. @RequestMapping(value = "/test.action")
  89. public String test(HttpServletRequest request, LoginCommand loginCommand) {
  90. System.out.println("用戶名:" + loginCommand.getUserName() + "--密碼:"
  91. + loginCommand.getPassword());
  92. User user = new User();
  93. user.setUserName(loginCommand.getUserName());
  94. user.setPassword(loginCommand.getPassword());
  95. userService.save(user);
  96. request.getSession().setAttribute("user", user);
  97. return "main";
  98. }
  99. }

當首次加載時,tree如下圖所示:
當點擊展開【節點1】和【節點3】時,如下圖所示:

當點擊tree節點時,會彈出:


4. 其他


瀏覽器:http://localhost:8080/MainFrameTest/index.action ,執行該程序。

spring mvc配置:

   
   
   
           
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  12. <!-- 掃描web包,應用Spring的注解 -->
  13. <context:component-scan base-package="com.ll.web" />
  14. <mvc:annotation-driven />
  15. <!-- 配置視圖解析器,將ModelAndView及字符串解析為具體的頁面 -->
  16. <bean
  17. class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  18. p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/jsp/"
  19. p:suffix=".jsp" />
  20. <!-- bean 視圖解析器 -->
  21. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
  22. p:order="10" />
  23. <!-- 返回tree-json 狀態 -->
  24. <bean id="treeJsonBean"
  25. class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
  26. <property name="renderedAttributes">
  27. <set>
  28. <value>treeJson</value>
  29. </set>
  30. </property>
  31. </bean>
  32. </beans>





附件列表

     


    免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM