ExtJs+JSP構造動態樹

來源:互聯網
上載者:User

ExtJs+JSP構造動態樹
在實際生產中樹的構造是需要根據後台資料庫中存放的內容動態載入的,例如一個系統中,不同的角色有不同的許可權,他們所能進行的操作也是不同的,如果需要構造一個樹形結構展現某個角色所能進行的操作,這就用到了樹形結構。
ExtJs提供了動態載入樹形結構的機制,這裡就用到了TreeStore動態載入,常用的動態載入資料格式有JSON和XML。
這裡我們看一下如何使用JSON格式資料動態載入屬性結構。

首先看一下建立的TreeStore。

var tree_store = Ext.create('Ext.data.TreeStore', {    id:'tree_store',    proxy: {    type: 'ajax',    url: 'MyJsp.jsp',    }});

這裡我們通過ajax的方式向一個JSP請求資料。下面看TreePanel的定義。

var treePanel = Ext.create('Ext.tree.Panel', {    id: 'tree_panel',    title: '動態載入',    region: 'west',    width: 200,    rootVisible: false,    store: tree_store,    listeners : {itemclick : tree_itemclick}, tools: [{        type: 'refresh',        tooltip: '重新整理',        handler: function() {//Ext.data.Store load            tree_store.load({                scope: this,                callback: function(records, operation, success) {                    treePanel.getRootNode().eachChild(function(child) { child.expand(); });                }            });        }    }    ],});
這裡需要注意一點,這段代碼是卸載onReady()函數中的,而且在載入treePanel之前調用tree_store的load方法。或者將tree_store寫在treePanel裡面,否則可能造成樹形結構無法正常載入顯示。

後台最主要的作用是將節點資訊組成恰當的JSON字串,如果需要載入的節點不多的話可以使用拼接字串的方式,如果節點較多可以使用javaBean封裝節點資訊,然後通過迴圈的方式拼接。

下面我們先看一下節點類的定義。

public class Node {private String text;//節點顯示字串private String id;//節點IDprivate boolean 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;}}

真實開發中可以從資料庫中取出這些節點的一個集合資訊,這裡我們使用一個普通方法類比一個節點資訊的集合。

public ArrayList getRes(){ArrayList result = new ArrayList();Node node1 = new Node();node1.setId("第一個節點的id");node1.setLeaf(true);node1.setText("第一個節點");Node node2 = new Node();node2.setId("第二個節點的ID");node2.setLeaf(true);node2.setText("第二個節點");result.add(node1);result.add(node2);return result;}

下面我們看一下如何根據這個集合來拼接JSON字元

public String getJson(){String result = "[{text:'1級檔案夾',id:'host',children:[";ArrayList arrlist = getRes();Iterator iterator = arrlist.iterator();while(iterator.hasNext()){Node node = iterator.next();if(iterator.hasNext())result +="{id:'"+node.getId()+"',text:'"+node.getText()+"',leaf:"+node.isLeaf()+"},";elseresult +="{id:'"+node.getId()+"',text:'"+node.getText()+"',leaf:"+node.isLeaf()+"}";}result+="]},{text:'2級檔案夾',id:'operate',children:[{id:'addhost',text:'第三個節點',leaf:true},"+ "{id:'addclient',text:'第四個節點',leaf:true}]}]";return result;}
在result字串定義和最後返回之前是使用最普通的字串拼接的方法,這裡一起進行測試。

然後我們看一下url中請求的JSP頁面。

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");String json = service.getJson();System.out.println(json);response.getWriter().write(json);%>
這樣就可以在請求頁面的過程中根據後台中的內容動態載入屬性結構。

下面看一下:




使用這種方式動態載入時將所有的幾點資訊一次全部載入,也就是當你點擊展開一級檔案夾或二級檔案夾的時候他不會在想後台發送請求,這裡通過firebug就能看出,如果樹的層次比較深,在後台構造JSON字串發送就會比較麻煩,這時可以使用非同步載入,也就是當頁面第一次載入的時候只會載入1級檔案夾和2級檔案夾,當點擊展開1級檔案夾或2級檔案夾的時候這時會繼續向後退發送請求,請求載入1級檔案夾的子節點。關於非同步載入將在下篇部落格介紹。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.