用ExtJS 实现动态载入树(Load tree)
来源:互联网 发布:卧龙生风雨燕归来知乎 编辑:程序博客网 时间:2024/05/19 20:44
Oracle DDL脚本 :
create table ORGANIZATION(
ORGID NUMBER(10) not null,
PARENTID NUMBER(10) not null,
ORGNAME VARCHAR2(32) not null,
ISAVAILABLE NUMBER(1) default 1 not null
);
alter table ORGANIZATION
add constraint PK_ORGID primary key (ORGID);
alter table ORGANIZATION
add constraint FK_ORGID_PARENTID foreign key (PARENTID)
references ORGANIZATION (ORGID);
初始化数据内容(注意第一行数据是必需的):
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (-100, -100, '组织机构图', 1);
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (1, -100, '公司总部1', 1);
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (2, -100, '公司总部2', 1);
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (3, -100, '公司总部3', 1);
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (4, 1, '公司总部1-1', 1);
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (5, 1, '公司总部1-2', 1);
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (6, 2, '公司总部2-1', 1);
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (7, 2, '公司总部2-2', 1);
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (8, 3, '公司总部3-1', 1);
insert into LOON_ORGANIZATION (ORGID, PARENTID, ORGNAME, ISAVAILABLE) values (9, 3, '公司总部3-2', 1);
有了数据库支持就可以动态的从数据库中提取树数据。
第一步是建立JSP文件(org.jsp)和JavaScript(org.js)文件:
在org.jsp中导入ExtJS所必需的库文件,并在<body>中加入
<body>
<div id="tree-div" style="overflow:auto; height:300px;width:200px;border:2px solid #c3daf9;"></div>
</body>
Org.jsp文件完全可以是静态HTML文件,这里org.jsp中不包含任何动态内容,注意ExtJS所必需的库文件类库路径问题。
Org.js文件内容:
Ext.onReady(function() {
var Tree = Ext.tree;
var tree = new Tree.TreePanel( {
el : 'tree-div',//目标div容器
autoScroll : true,
animate : true,
enableDD : true,
containerScroll : true,
loader : new Tree.TreeLoader( {
dataUrl : ' OrgTreeJsonData.action '// OrgTreeJsonData.action就是要动态载入数据的请求地址,这里请求时会提交一个参数为node的值,值为root即new Tree.AsyncTreeNode()对象的id值
})
});
var root = new Tree.AsyncTreeNode( {
text : '组织机构树',
draggable : false,
id : '-100'//默认的node值:?node=-100
});
tree.setRootNode(root);
tree.render();
root.expand();
});
OrgTreeJsonData.action所请求的JSON数据例子:
[ {
"text" : "公司总部1",
"id" : "1",
"cls" : "folder"
}, {
"text" : "公司总部2",
"id" : "2",
"cls" : "folder"
}, {
"text" : "公司总部3",
"id" : "3",
"cls" : "folder"
}]
服务器端可以使用这样的SQL语句来查询:
select t.orgid,t.orgname,t.parentid from organization t
where t.parentid='-100' and t.orgid!='-100'
下面的代码片断用于struts2 action类中:
public String treeNode() {
try {
List<Object[]> list = this.organizationService.findByParentId(this.node);
if (list != null && !list.isEmpty()) {
boolean isFirst = true;
int i = 0;
int last = list.size();
for (Object[] o : list) {
if (i == 0) {
this.setJsonString("[{/"text/" :/"" + o[1].toString() + "/" ,/"id/" :/"" + o[0].toString()
+ "/" ,/"cls/" :/"folder/"} ");
} else if (i == (last - 1)) {
this.setJsonString(this.getJsonString() + ",{/"text/" :/"" + o[1].toString() + "/" ,/"id/" :/""
+ o[0].toString() + "/" ,/"cls/" :/"folder/"}]");
} else {
this.setJsonString(this.getJsonString() + ",{/"text/" :/"" + o[1].toString() + "/" ,/"id/" :/""
+ o[0].toString() + "/" ,/"cls/" :/"folder/"}");
}
i++;
}
} else {
this.setJsonString("");
}
System.out.println(this.getJsonString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return this.INPUT;
}
return this.SUCCESS;
}
运行时的图:
- 用ExtJS 实现动态载入树(Load tree)
- 用ExtJS 实现动态载入树(Load tree)
- 用ExtJS 实现动态载入树(Load tree)
- 【转】用ExtJS 实现动态载入树(Load tree)
- 用ExtJS 实现动态载入树(Load tree)(转载)
- Ext2.0 + .Net 2.0应用实例(2)--用ExtJS 实现动态载入树
- extjs的tree的使用(拖动、动态载入json数据、拖动后的事件处理)
- extjs的tree的使用(拖动、动态载入json数据、拖动后的事件处理)
- Extjs+jquery实现动态载入多个页面,实现iframe效果
- Extjs 动态Tree
- 用Jquery.load载入页面
- Extjs tree 动态树的实现和右键编辑等操作
- EXTJS动态树的实现
- EXTJS动态树的实现
- MATLAB中使用load动态载入mat文件
- 利用ExtJS Tree的TreeNode(Json格式)在Struts 2中实现Ajax真正的动态异步加载树
- EXTJS实现的动态树举例
- EXTJS动态树的实现举例
- 第 6 章 奔腾吧!让不同的浏览器里显示一样的布局
- 第 7 章 低鸣吧!拖拽就像呼吸一样容易
- 第 8 章 哭泣吧!现在才开始讲基础问题
- 第 9 章 沉寂吧!我们要自己的控件
- 博弈
- 用ExtJS 实现动态载入树(Load tree)
- 新年贺辞
- ExtJS与.NET结合开发实例(Grid之数据显示、分页、排序篇)
- ExtJS与.NET结合开发实例(Grid之批量删除篇)
- ExtJS与.NET结合开发实例(Grid之新增——Form提交篇)
- EXT调用ASP.NET AJAX WebService
- css3.0抢先看(二):border-color边框颜色的新用法,绝对酷
- 走回原点
- 微软洽购雅虎事件对中国网络经济影响解读