日期:2014-05-16  浏览次数:20443 次

ExtJS动态创建树结构

??

????? 这两天在研究ExtJS,对其有了深刻的认识。总结如下:

?

?????????? ?根据显示数据量,决定Ext树的类型。如果是大数据量,一般采用创建Ext.tree.AsyncTreeNode根节点的方式

?

(即每点击一个树节点后,动态的从后台读取数据。优点:加快显示速度) 。如果数据量小,一般采用Ext.tree.TreeNode

?

这样的根节点。

?

????? 第一种代码如下:

????

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="../extjs/resources/css/ext-all.css"/>
<script type="text/javascript" src="../extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../extjs/ext-all.js"></script>
<title>树结构</title>
<script type="text/javascript">
Ext.onReady(function() {
	var root = new Ext.tree.AsyncTreeNode({
				id : "root",
				text : "树的根"
	});

	var loader = new Ext.tree.TreeLoader({
				url : '<%=request.getContextPath()%>/User_getNodeTree'
	});
	
	loader.on("beforeload", function(loader, node) {
		     alert("---"+node.id);
			loader.baseParams.nodeId = node.id;
	});

	var tree = new Ext.tree.TreePanel({
				renderTo : "tree",
				root : root,
				loader : loader,
				width : 200,
				height : 300
			});
});
</script>
</head>
<body>
 <!-- 要有一定高度Ext2.0,不然看不见 -->
	<div id="tree" style="overflow:auto; height:100%;width:250px;border:5px solid #c3daf9;"></div> 
</body>
</html>

?

??? 实体描述:构造包括 id,text,cls,leaf,href属性的实体

?????????

?

????后台返回数据:

??

?????????? 每次点击一个树节点后,从后台读取该节点的子节点的信息。例如:

????????

????????????? [{text:'用户',href:'test.jsp',leaf:true},
????????????? ?{text:'管理员',href:'##',leaf:true}]

?

?

第二种树:

?

???页面:

?

?????

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="../extjs/resources/css/ext-all.css"/>
<script type="text/javascript" src="../extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../extjs/ext-all.js"></script>
<title>树结构</title>
<script type="text/javascript">
Ext.onReady(function() {

var treePanel=new Ext.tree.TreePanel({
           renderTo : "tree",
            useArrows:true,
            autoScroll:true,
            animate:true,
            enableDD:true,
           //是否显示跟节点 
           rootVisible:false,
           frame: true,
           containerScroll: true
           
        });

 var root=new Ext.tree.AsyncTreeNode({
		            id:'4',
		            text:'root',
		            leaf:false,
		            loader:new Ext.tree.TreeLoader({dataUrl:'/User_getNodeTree'})});
			 
            treePanel.setRootNode(root);
           treePanel.getRootNode().expand(true);

});
</script>
</head>
<body>
 <!-- 要有一定高度Ext2.0,不然看不见 -->
	<div id="tree" style="overflow:auto; height:100%;width:250px;border:5px solid #c3daf9;"></div> 
</body>
</html>

????????

????????

??? 实体类:

?

??????

package com.order.entity;

import java.util.List;


public class TreeModule{
	/*
	* 此bean用于构建树向其中注入有关的信息,将SysModule中的信息拷贝到JsonTreeModule中
	*/
	private String id; //ID 
	private String text; //节点显示 
	private String cls; //图标 
	private boolean leaf; //是否叶子 
	private String href; //链接 
	private String hrefTarget; //链接指向 
	private boolean expa