Extjs tree 异步加载 实例
一:jsp 页面
<%@ page contentType="text/html; charset=UTF-8" language="java"
import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="com.luguang.model.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<base href="<%=basePath%>">
<title>组织树</title>
<link rel="stylesheet" href="<%=basePath%>scripts/extjs/resources/css/ext-all.css" type="text/css"/>
<script type="text/javascript" src="<%=basePath%>scripts/extjs/ext-all.js"></script>
<script LANGUAGE="JavaScript">
Ext.require(['*']);
Ext.onReady(function(){
//分级异步加载节点实例
//定义数据模型
Ext.regModel("orgInfo0",{
fields:['orgId','text']
});
var myOrgStore=new Ext.data.TreeStore({
model:'orgInfo0',
nodeParam:'orgId',//指定节点参数名
proxy:{
type:'ajax',
url:'<%=basePath%>/org/showOrgTreeExtJsTree',
reader:'json'
},
autoLoad:true,
root:{
text:'${sessionScope.parOrgName}',
id:'org_${sessionScope.parOrgId}'
}
});
function tree_itemclick0(node,event){
var nodeId=event.data.id;
var nodeText=event.data.text;
if(nodeId.indexOf("org")>=0){
nodeId=nodeId.substring(4,nodeId.length);
$("#orgName0").val(nodeText);
$("#orgId0").val(nodeId);
}
}
var orgPointTree=Ext.create('Ext.tree.Panel',{
renderTo:'pointOrgTree',
dataIndex:'text',
sortable:true,
store:myOrgStore,
rootVisible:true,
listeners:{
itemclick:tree_itemclick0,
afterrender:function(orgPointTree){
setTimeout(function(){var rootNode=orgPointTree.getRootNode(); rootNode.expand(); },10);
}
}
});
});
</script>
</head>
<body style="border-bottom-width:thin">
<div id="pointOrgTree"></div>
</body>
</html>
后台:org/showOrgTreeExtJsTree方法:
String result="";
String orgId=request.getParameter("orgId");
String[] oIds=orgId.split("_");
String rId=request.getSession().getAttribute("rId0").toString();
if(rId!=null && !rId.equals("")){
if(oIds.length==2){
if(oIds[0].equals("org")){
int oid=Integer.parseInt(oIds[1].toString());
//子组织
String sql="select org.ORG_NAME ,org.ORG_ID from lgvmp_org org,lgvmp_role_org_relation roleOrg where org.PARENT_ID="+oid+" and org.ORG_ID=roleOrg.ORG_ID and roleOrg.ROLE_ID="+rId+" order by org.ORDER_BY";
Session sess= this.getEntityDao().getHibernateTemplate().getSessionFactory().openSession();
ArrayList<Object[]> orgArr=(ArrayList<Object[]>) sess.createSQLQuery(sql).list();
sess.close();
result="[";
for(Object[] obArr:orgArr){
result=result+"{text:'"+obArr[0].toString()+"',id:'org_"+Integer.parseInt(obArr[1].toString())+"'},";
}
if(result.endsWith(",")){
result=result.substring(0,result.length()-1);
}
result+="]";
}
}
}
response.getWriter().write(result);