首頁 >web前端 >js教程 >jquery easyui tree非同步載入子節點詳解

jquery easyui tree非同步載入子節點詳解

小云云
小云云原創
2018-01-23 10:02:492489瀏覽

easyui中的樹可以從標記建立,也可以透過指定一個URL屬性讀取資料建立。如果想要建立一棵非同步樹,需要為每個節點指定一個id屬性值,這樣在載入資料時會自動向後台傳遞id參數。本文主要介紹解析jquery easyui tree非同步載入子節點問題,easyui中的樹可以從標記建立,也可以透過指定一個URL屬性讀取資料建立,有興趣的可以了解一下。希望能幫助大家。


    #編寫前台程式碼:

    $('#tt').tree({
      url:'/demo2/node/getNodes'  // The url will be mapped to NodeController class and getNodes method
    });

    為測試用,建立一個節點的資料模型:

    @Table(name="nodes")
    public class Node extends ActiveRecordBase{
      @Id public Integer id;
      @Column public Integer parentId;
      @Column public String name;
     
      public boolean hasChildren() throws Exception{
        long count = count(Node.class, "parentId=?", new Object[]{id});
        return count > 0;
      }
    }

    寫後台的控制器程式碼:

    public class NodeController extends ApplicationController{
      /**
       * get nodes, if the 'id' parameter equals 0 then load the first level nodes,
       * otherwise load the children nodes
       * @param id the parent node id value
       * @return the tree required node json format
       * @throws Exception
       */
      public View getNodes(int id) throws Exception{
        List<node> nodes = null;
     
        if (id == 0){  // return the first level nodes
          nodes = Node.findAll(Node.class, "parentId=0 or parentId is null", null);
        } else {  // return the children nodes
          nodes = Node.findAll(Node.class, "parentId=?", new Object[]{id});
        }
     
        List<map>> items = new ArrayList<map>>();
        for(Node node: nodes){
          Map<string> item = new HashMap<string>();
          item.put("id", node.id);
          item.put("text", node.name);
     
          // the node has children, 
          // set the state to 'closed' so the node can asynchronous load children nodes 
          if (node.hasChildren()){
            item.put("state", "closed");
          }
          items.add(item);
        }
     
        return new JsonView(items);
      }
    }</string></string></map></map></node>

    官網範例網址:http://www.jeasyui.com/tutorial/tree/tree2.php

    demo下載:easyui-tree2_jb51.rar 

    重要的事情說三遍!!!

    $('#tt').tree({
      method:"POST",
      url:'/demo2/node/getNodes'  // The url will be mapped to NodeController class and getNodes method
    });

    method一定要用POST,GET的話要在URL後面用一個變數來做時間戳處理。

    相關推薦:

    關於jQuery外掛zTree實作刪除樹節點方法詳解

    jQuery外掛zTree實作的多重選擇樹效果實例說明

    jquery ztree實作右鍵收藏功能實例詳解        

    #######

    以上是jquery easyui tree非同步載入子節點詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn