ExtJs通過後台動態建立樹

來源:互聯網
上載者:User

ExtJs通過後台動態建立樹

ExtJs動態建立樹的方法是:當樹的TreeStore要載入的時候,通過ajax請求後台資料,根據後台返回資料動態載入樹結構。

正常情況下我們定義TreeStore是這樣定義的:

 

var tree = Ext.create('Ext.data.TreeStore', {root : {expanded : true,children : [ {text : "操作",id : "operator",expanded : true,children : [     {text : "第一個葉子",leaf : true,id : "termOpera"},    {text : "第二個葉子",leaf : true,id : "userMang"},     {text : "第三個葉子",leaf : true,id : "importExcel"} ]} ]}});
這樣是直接定義好樹結構,我們看一個樹的節點有以下屬性:text(顯示文本)、leaf、id……屬性。

 

現在如果需要動態載入樹形結構就需要通過ajax請求後台資料。這裡我們看一下如何定義TreeStore

 

var tree_store = Ext.create('Ext.data.TreeStore', {    id:'tree_store',    proxy: {        type: 'ajax',        url: 'tree_load',        reader:{        root:'nodelist',    type:'json'    }    },    fields : [    {name:'text'},    {name:'id'},    {name:'leaf'},    ]});
這樣就定義了ajax請求後台資料載入這個TreeStore,這樣在初始載入的時候就會請求url--tree_load,並且傳入根節點的id,如果樹中某個節點不是葉子節點,那麼在點擊展開該節點時候會也會傳入該節點的id。

 

這裡我們同過Struts2攔截請求,並對請求處理,首先在後台定義一個Node類,這是一個純javaBean。有id、text、leaf屬性。

 

public class Node {private String text;private String id;private boolean leaf;public Node(){}public Node(String text,String id,boolean leaf){this.text = text;this.id = id;this.leaf = leaf;}public String getText() {return text;}public void setText(String text) {this.text = text;}public String getId() {return id;}public void setId(String id) {this.id = id;}public boolean isLeaf() {return leaf;}public void setLeaf(boolean leaf) {this.leaf = leaf;}}

然後我們看處理該請求的Action類

 

 

public class BaseAction extends ActionSupport {private static final long serialVersionUID = 1L;private String id;private ArrayList nodelist;public String getId() {return id;}public void setId(String id) {this.id = id;}public ArrayList getNodelist() {return nodelist;}public void setNodelist(ArrayList nodelist) {this.nodelist = nodelist;}private NodeService ns = new NodeService();public String treeload(){System.out.println(id);nodelist = ns.getTree(id);return SUCCESS;}}

id屬性用於接收點擊展開節點的id,nodelist是返回結果,是Node類的集合。

 

這裡我們在後台直接建立對象方式進行類比,對於擷取Node的方式可以從資料庫中擷取資料。

 

public class NodeService {//這裡做測試,後期節點資訊需要從資料庫中存取public ArrayList getTree(String id){ArrayList arr = new ArrayList();if("root".equals(id)){Node node = new Node("監控主機1","one", "one", false,null);Node node2 = new Node("監控主機2","second", "second", false,null);arr.add(node);arr.add(node2);}else{Node mode = new Node("串連檢測", null,"link", true,id);Node mode1 = new Node("系統檢測",null,"system", true,id);Node mode3 = new Node("硬碟檢測", null,"disk", true,id);Node mode4 = new Node("應用檢測", null,"app", true,id);Node mode5 = new Node("資料庫檢測",null,"db", true,id);Node mode6 = new Node("備份檢測", null,"bak", true,id);Node mode7 = new Node("流量檢測", null,"netcard", true,id);arr.add(mode);arr.add(mode1);arr.add(mode3);arr.add(mode4);arr.add(mode5);arr.add(mode6);arr.add(mode7);}return arr;}}
下面看運行效果

 


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.