发起跳转到树形结构的js代码:
function addNew(){
var pageUrl = "${root}sms/jsp/smsTemplateInfoNew.jsp?gid="+${sTemGroupId};
openNewWindow("添加短信模版",pageUrl,800,480);
}
///smsTemplateInfoNew.jsp
发起代码:
function ShowCallType(){
document.all.iframeWorkTypeArea.src="workTypeNew.jsp?parentTypeId=0"; //这个是不用使用下拉列表,直接把下拉列表的选项值直接变为树
}
///workTypeNew.jsp
生成树的关键1:
<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<jsp:include page="/common/ext2/ext-all.jsp" flush="true" />
<html>
<head>
<body>
<div id="sdiv" style="overflow:auto; height:100%;width:100%;"></div>
</body>
</html>
<script type="text/javascript">
<!--
Ext.onReady(function(){
var Tree = Ext.tree;
var tree = new Tree.TreePanel({
el:'sdiv',
useArrows:true,
autoScroll:true,
animate:true,
containerScroll: true,
rootVisible:false,
dataUrl: '../../workflow/jsp/buildWorkTypeTreeNew.action?parentTypeId=<%=request.getParameter("parentTypeId")%>',
root: {
nodeType: 'async',
text: '工单类型',
draggable:false,
id:'0'
}
});
// render the tree
tree.render();
// tree.getRootNode().expand();
});
var workTypeId = "";
var workTypeStr= "";
function changeWorkType(node){
// alert("tag="+tag);
if(node == null){
return;
}else{
workTypeStr= ""
orderTypeDo(node);
parent.addSelectCallItem(node.id,workTypeStr);
workTypeId = node.id;
}
}
function orderTypeDo(node){
workTypeStr = "."+node.text+workTypeStr;
if(node.parentNode !=null && node.parentNode.id !='0'){
orderTypeDo(node.parentNode);
}
}
function clearWorkType(node){
workTypeId = node.id;
}
function checks2(node,checked){
// alert("确定选择!");
//alert("checked:"+checked);
var flag=true;
var pn=node.parentNode;
node.eachChild(function(child) {
if(child.attributes.checked==false){
flag=false;
}
})
//当父接点被选种,将展开并子接点也都被选种
node.expand();
if(checked==false&&flag==false){
}else{
/
if(checked==true){
if(!(node.hasChildNodes())){
changeWorkType(node);
}
}
if(checked==false){
if(!(node.hasChildNodes())){
parent.removeSelectCallItem2(node.id);
}
}
///
node.eachChild(function(child) {
child.ui.toggleCheck(checked);
child.attributes.checked = checked;
child.fireEvent('checkchange', child, checked);
})
}
//当子节点都被选中该接点也被选种,不能与下面的IF调换位置
if(pn.attributes.checked==false){
var flag1=true;
pn.eachChild(function(child) {
if(child.attributes.checked==false){
flag1=false;
}
})
if(flag1==true){
pn.ui.toggleCheck(flag1);
}
}
//当子接点有未被选种的父接点也被置为false
if(pn.attributes.checked==true||pn.attributes.checked==false){
if(checked==false){
pn.ui.toggleCheck(checked);
}
}
}
//-->
</script>
//Struts.xml代码/
<action name="buildWorkTypeTreeNew" class="workTypeAction" method="buildWorkTypeTreeNew">
<result name="success">ajaxMessageNew.jsp</result>
</action>
//ajaxMessageNew.jsp代码///
<%@ page contentType="text/html; charset=utf-8" %>[${workTypeTreeNew}]
//workTypeAction.java代码///
/**
* 生成树
* */
public String buildWorkTypeTreeNew(){
WorkTypeManager manager = (WorkTypeManager) commonModelMaintain;
try{
workTypeTreeNew = manager.getWorkTypeByRootNew(parentTypeId);
}catch(Exception ex){
log.error(ex.getMessage());
}
return SUCCESS;
}
//WorkTypeManager.java代码///
生成树的关键2:
/**
* 取节点列树形结构
* @param rootId
* @return
*/
public String getWorkTypeByRootNew(String rootId){
String result ="";
if(rootId == "" ) rootId ="0";
WorkflowWorkType workType = workTypeDAO.loadById(rootId); //从数据库取得有父子关系的数据(以供生成树)
String textStr = "";
if("0".equals(rootId) || "".equals(rootId)){
workType = null;
textStr ="工单类型";
}else if( workType !=null && workType.getTypeName() !=null ){
textStr = workType.getTypeName();
}else{
return result;
}
Iterator itor = null;
List treeInfo = workTypeDAO.findByRootId(rootId);
if (treeInfo != null && treeInfo.size() > 0) {
if(rootId != "0" && !"".equals(rootId)){
result="{"
+"id:'"+rootId+"',"
+"singleClickExpand:'"+true+"'," //使成长的树,当单击相应的文字导航时能展开其下的子菜单,而不需双击
+"expanded :false," //导航树是否默认展开
+"animate : true,"
+"collapsible : true,"
+"enableDD : true,"
+"enableDrag : true,"
// +"allowDrag : false,"
+"checked : false," //指定使用复选框
// +"rootVisible : true,"
// +"autoScroll : true,"
// +"autoHeight : true,"
// +"lines : true,"
/*****tangyu现加了getOrderTemplate();方法******************************************/
+"text:'"+textStr+"',listeners:{'checkchange':function(node,checked){checks2(node,checked); }},"
// +"text:'"+textStr+"',listeners:{'click':function(node,event){event.stopEvent();clearWorkType(node); }},"
+"children:";
}
itor = treeInfo.iterator();
int rowInt = 0;
while (itor.hasNext()) {
rowInt++;
WorkflowWorkType cInfo = (WorkflowWorkType) itor.next();
String intChildId = cInfo.getTypeId();
if(rootId == "0" || "".equals(rootId)){
result = result +getWorkTypeByRootNew(intChildId);
if(rowInt < treeInfo.size()){
result = result + ",";
}
}
//当记录只有一条时
else if (treeInfo.size() == 1)
result = result + "["+ getWorkTypeByRootNew(intChildId) + "]";
//多条记录
else if (treeInfo.size() > 1) {
if (rowInt == treeInfo.size())
result = result + getWorkTypeByRootNew(intChildId) + "]";
else if (rowInt == 1)
result = result + "["+ getWorkTypeByRootNew(intChildId) + ",";
else
result = result + this.getWorkTypeByRootNew(intChildId) + ",";
}
}
if(rootId != "0"){
result +="}";
}
}else {//该节点为叶子节点
result = "{" + "id:" + rootId + ",allowDrag : false, checked : false," + "text:'" + textStr + "',leaf:'undefind' ,listeners:{'checkchange':function(node,checked){checks2(node,checked); }}"+ "}";
}
return result;
}
附加 hibernate配置文件WorkflowWorkType.hbm.xml//
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.skylink.csms.workflow.model.WorkflowWorkType" table="WF_WORKTYPE_T" >
<id name="typeId" type="java.lang.String">
<column name="TYPE_ID" length="20" />
<generator class="com.skylink.common.hibernate.generator.StringIncrease" />
</id>
<many-to-one name="parentType" class="com.skylink.csms.workflow.model.WorkflowWorkType" fetch="select">
<column name="PARENT_ID" precision="22" scale="0" />
</many-to-one>
<property name="typeName" type="java.lang.String">
<column name="TYPE_NAME" length="64" />
</property>
<property name="memo" type="java.lang.String">
<column name="MEMO" length="256" />
</property>
<many-to-one name="workflowFlowInfo" class="com.skylink.csms.workflow.model.WorkflowFlowInfo" fetch="select">
<column name="WF_ID" length="8" />
</many-to-one>
<property name="levelNum" type="java.lang.Long">
<column name="LEVEL_NUM" length="2" />
</property>
<property name="status" type="java.lang.String">
<column name="STATUS" length="2" />
</property>
<property name="seq" type="java.lang.Float">
<column name="SEQ"/>
</property>
</class>
</hibernate-mapping>