1:前台js代码如下:
Ext.QuickTips.init();// 浮动信息提示 var Tree = Ext.tree; // 定义根节点的Loader var treeloader = new Tree.TreeLoader({ // dataUrl : 'tree.jsp'//这里可以不需要指定URL,在加载前事件响应里面设置 }); // 添加一个树形面板 var treepanel = new Tree.TreePanel({ title:'所有栏目如下:', width : 230, height : 500, split : true, autoScroll : true, autoHeight : false, expanded : true, frame : true,// 美化界面 enableDD : false,// 是否支持拖拽效果 containerScroll : true,// 是否支持滚动条 rootVisible : false, // 是否隐藏根节点,很多情况下,我们选择隐藏根节点增加美观性 border : true, // 边框 animate : true, // 动画效果 useArrows:true, lines:true, //节点间的虚线条, loader : treeloader // 树加载 }); // 异步加载根节点 var rootnode = new Tree.AsyncTreeNode({ id : '0', text : '河南省', cls:'folder', draggable : false, expanded : false }); // 为tree设置根节点 treepanel.setRootNode(rootnode); treepanel2.setRootNode(rootnode2); // 响应加载前事件,传递node参数 treepanel.on('beforeload', function(node) { treepanel.loader.dataUrl = 'extTypeTree.action?parentId=' + node.id; // 定义子节点的Loader }); // 渲染树形 // 展开节点,第一个参数表示是否级联展开子节点 rootnode.expand(false); // 设置树的点击事件 function treeClick(node, e) { Ext.getCmp('updateb').disable(); Ext.getCmp('deleteb').disable(); p2=0; if(rootnode2.findChild('id',node.id)==null){ Ext.getDom('typename').value=node.text; Ext.getDom('typecode').value=node.id; Ext.getCmp('addb').enable(); clickUMTree(); }else{ Ext.Msg.alert('提醒','该用户已经拥有此栏目的管理权限!'); } }
2:action
package com.dingxun.actions; import java.util.ArrayList; import java.util.List; import net.sf.json.JSONArray; import com.dingxun.basemodel.JSONTreeNode; import com.dingxun.db.BaseDao; public class JsonTreeAction extends PmBaseAction { private static final long serialVersionUID = 1L; private String parentId; private String JonTreeString; private BaseDao dao = new BaseDao(); private String JsonString; public String execute() { List<JSONTreeNode> treeNodeArray = null; String SQLString = "SELECT PCODE,PNAME FROM DX_FM_XZ WHERE PCODE LIKE ? ORDER BY PCODE"; String PSQLString = "SELECT PCODE FROM DX_FM_XZ WHERE LENGTH(trim(PCODE))<9 ORDER BY PCODE"; String[] params = new String[] { parentId }; StringBuffer parentIDBuffer = new StringBuffer(); parentIDBuffer.append("|"); String parentIDString = null; List jsonList = new ArrayList(); List listpcode = dao.QueryBySql(PSQLString, 0, 0); for (int i = 0; i < listpcode.size(); i++) { Object[] obj = (Object[]) listpcode.get(i); String id = obj[0].toString(); parentIDBuffer.append(id); parentIDBuffer.append("|"); parentIDString = parentIDBuffer.toString(); } List listxz = dao.QueryByParamSql(SQLString, 0, 0, params); treeNodeArray = new ArrayList<JSONTreeNode>(); for (int j = 0; j < listxz.size(); j++) { Object[] obj = (Object[]) listxz.get(j); String id = obj[0].toString(); String text = obj[1].toString(); JSONTreeNode treeNode = new JSONTreeNode(); treeNode.setId(id); treeNode.setText(text); if (parentIDString.indexOf("|" + id + "|") >= 0) // 父节点 { treeNode.setCls("x-html-editor-tip"); treeNode.setLeaf(false); treeNode.setExpanded(false); } else // 子节点 { treeNode.setCls("x-html-editor-tip"); treeNode.setLeaf(false); treeNode.setExpanded(false); } treeNodeArray.add(treeNode); } JSONArray JsonArray = JSONArray.fromObject(treeNodeArray); // 得到JSON数组 JsonString = JsonArray.toString();//返回JSON数据 return "success";// 返回JSON数据 } public String getParentId() { return parentId; } public void setParentId(String parentId) { if (parentId.length() == 6) { this.parentId = parentId + "___"; } else if (parentId.length() == 4 || parentId.length() == 2) { this.parentId = parentId + "__"; } } public String getJonTreeString() { return JonTreeString; } public void setJonTreeString(String jonTreeString) { JonTreeString = jonTreeString; } public String getJsonString() { return JsonString; } public void setJsonString(String jsonString) { JsonString = jsonString; } }
3:jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <s:property value="JsonString" escape="false"/>
4:json节点
package com.dingxun.basemodel; public class JSONTreeNode { private String id; //ID private String text; //节点显示 private boolean leaf; //是否叶子 private String href; //链接 private String cls; //图标 private String hrefTarget; //链接指向 private boolean expanded; //是否展开 private String description; //描述信息 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getCls() { return cls; } public void setCls(String cls) { this.cls = cls; } public boolean isLeaf() { return leaf; } public void setLeaf(boolean leaf) { this.leaf = leaf; } public String getHref() { return href; } public void setHref(String href) { this.href = href; } public String getHrefTarget() { return hrefTarget; } public void setHrefTarget(String hrefTarget) { this.hrefTarget = hrefTarget; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public boolean isExpanded() { return expanded; } public void setExpanded(boolean expanded) { this.expanded = expanded; } }