数据库表设计
DROP TABLE treenode;
CREATE TABLE treenode (
tid int NOT NULL PRIMARY KEY,
parentid int ,
text char(16) NOT NULL,
href char(32)
);
INSERT INTO treenode (tid, parentid, text, href) VALUES(-100, -100,'01','hello.html');
INSERT INTO treenode (tid, parentid, text, href) VALUES(1, -100,'01','hello.html')
INSERT INTO treenode (tid, parentid, text, href) VALUES(2, -100,'02', 'hello.html')
INSERT INTO treenode (tid, parentid, text, href) VALUES(3, -100,'03', 'hello.html')
INSERT INTO treenode (tid, parentid, text, href) VALUES(4, 1,'04','hello.html')
INSERT INTO treenode (tid, parentid, text, href) VALUES(5, 1,'05','hello.html')
INSERT INTO treenode (tid, parentid, text, href) VALUES(6, 2,'02-01','hello.html')
INSERT INTO treenode (tid, parentid, text, href) VALUES(7, 2,'02-01', 'hello.html')
INSERT INTO treenode (tid, parentid, text, href) VALUES(8, 3,'03-01','hello.html')
INSERT INTO treenode (tid, parentid, text, href) VALUES(9, 3,'03-02','hello.html')
INSERT INTO treenode (tid, parentid, text,href) VALUES(10, 3,'03-03', 'hello.html')
Extjs代码:
Ext.onReady(function(){
var Tree = Ext.tree;
var treeloader=new Tree.TreeLoader({dataUrl:'servlet/TreeAction?TID=-100'});
//定义树的跟节点
var root=new Ext.tree.AsyncTreeNode({
id:"-100",//根节点id
draggable:false,
expanded :true,
text:"后台管理"
});
//生成树形面板
var tree=new Ext.tree.TreePanel({
renderTo:'Tree',
root:root,//定位到根节点
width:150,
height:300,
animate:true,//开启动画效果
enableDD:false,//不允许子节点拖动
autoScroll:true,
loader:treeloader
// border:false,//没有边框
// rootVisible:false//设为false将隐藏根节点,
});
tree.setRootNode(root);
tree.on('beforeload',
function(node){
tree.loader.dataUrl='servlet/TreeAction?TID='+node.id; //定义子节点的Loader
});
tree.render();
root.expand();
});
java代码:
//从数据库中返回父节点id号
String sql="SELECT parentid FROM treenode WHERE parentid>0 Group By parentid Order By parentid";
StringBuilder s= new StringBuilder();
PreparedStatement pst = null;
ResultSet rs = null;
try{
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next())
{
s.append('|');
s.append(rs.getInt("parentid"));
}
}catch(SQLException e){
e.printStackTrace();
}
return s;
}
//从数据库返回JSONArray字符串对象类
StringBuilder s=this.getResulsetRow(conn);
ArrayList al=new ArrayList();
StringBuilder node=new StringBuilder("[");
String sql="SELECT * FROM treenode WHERE parent);
return node;