溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

解析jquery easyui tree異步加載子節(jié)點(diǎn)問題

發(fā)布時(shí)間:2020-08-26 01:25:08 來源:腳本之家 閱讀:170 作者:龍果果 欄目:web開發(fā)

easyui中的樹可以從標(biāo)記中建立,也可以通過指定一個(gè)URL屬性讀取數(shù)據(jù)建立。如果想建立一棵異步樹,需要為每個(gè)節(jié)點(diǎn)指定一個(gè)id屬性值,這樣在加載數(shù)據(jù)時(shí)會(huì)自動(dòng)向后臺(tái)傳遞id參數(shù)。

<ul id="tt"></ul>

編寫前臺(tái)代碼:

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

為測(cè)試用,建立一個(gè)節(jié)點(diǎn)的數(shù)據(jù)模型:

@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;
  }
}

編寫后臺(tái)的控制器代碼:

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<String,Object>> items = new ArrayList<Map<String,Object>>();
    for(Node node: nodes){
      Map<String,Object> item = new HashMap<String,Object>();
      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);
  }
}

官網(wǎng)例子地址: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后面用一個(gè)變量來做時(shí)間戳處理。

method一定要用POST,GET的話要在URL后面用一個(gè)變量來做時(shí)間戳處理。

method一定要用POST,GET的話要在URL后面用一個(gè)變量來做時(shí)間戳處理。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI