1.节点对象
package com.oohla.uni.tag.ui.action;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.oohla.uni.database.biz.ContactGroup;
import com.oohla.uni.sysadmin.web.AppConfig;
public class ContactGroupNode extends ContactGroup{
private static final Log log = LogFactory.getLog(ContactGroupNode.class);
private ContactGroup contactGroup;
public ContactGroupNode(ContactGroup contactGroup){
this.contactGroup = contactGroup;
}
public int getId() {
return contactGroup.getGroupId();
}
public String getName() {
return contactGroup.getGroupName();
}
public List<ContactGroupNode> getChildren(){
log.info("getChildren");
try {
List<ContactGroup> contactGroups = AppConfig.getInstance().getContactBiz().findContactGroupsByRefGroupId(contactGroup.getGroupId());
if(contactGroups != null && contactGroups.size() > 0){
List<ContactGroupNode> nodes = new ArrayList<ContactGroupNode>();
for (int i = 0;i < contactGroups.size();i ++) {
ContactGroupNode contactGroupNode = new ContactGroupNode(contactGroups.get(i));
nodes.add(contactGroupNode);
}
return nodes;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
2.action
package com.oohla.uni.tag.ui.action;
import java.util.List;
import com.oohla.uni.database.biz.ContactBiz;
import com.oohla.uni.database.biz.ContactGroup;
import com.opensymphony.xwork2.ActionSupport;
public class TreeAction extends ActionSupport{
private ContactBiz contactBiz;
private ContactGroupNode contactGroupNode;
public void setContactBiz(ContactBiz contactBiz) {
this.contactBiz = contactBiz;
}
public ContactGroupNode getContactGroupNode() {
return contactGroupNode;
}
@Override
public String execute() throws Exception {
List<ContactGroup> contactGroups = contactBiz.queryPrivateContactsAndGroups(140);
contactGroupNode = new ContactGroupNode(contactGroups.get(0));
return "tree";
}
}
3.配置文件
<package name="tagPackage" namespace="/tags" extends="struts-default">
<action name="tree" class="treeAction">
<result name="tree">/tags/c_tree.jsp</result>
</action>
</package>
4.页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html>
<head>
<sx:head />
<script type="text/javascript">
//展开
function expand(){
var nodes =dojo.widget.manager.getWidgetsByType('struts:StrutsTreeNode');
for( var i=0; i < nodes.length; i++){
nodes[i].expand();
}
}
function collapse(){
var nodes =dojo.widget.manager.getWidgetsByType('struts:StrutsTreeNode');
for( var i=0; i < nodes.length; i++){
nodes[i].collapse();
}
}
function nodeClick(obj){
alert(obj);
}
dojo.addOnLoad(function() {
var t = dojo.widget.byId('group');
dojo.event.topic.subscribe(t.eventNames.expand, treeNodeExpanded);
dojo.event.topic.subscribe(t.eventNames.collapse, treeNodeCollapsed);
var s = t.selector;
dojo.event.connect(s, 'select', 'treeNodeSelected');
});
//选择节点触发事件
function treeNodeSelected(arg) {
alert(arg.source.title + ' selected');
}
//展开节点触发事件
function treeNodeExpanded(arg) {
alert(arg.source.title + ' expanded');
}
//闭合节点触发事件
function treeNodeCollapsed(arg) {
alert(arg.source.title + ' collapsed');
}
</script>
</head>
<body>
<a href="#" style="font-size:12px" onclick="expand();">展开</a> <a href="#" style="font-size:12px" onclick="collapse();">闭合</a>
<sx:tree id="group" rootNode="contactGroupNode" nodeTitleProperty="name"
nodeIdProperty="id" childCollectionProperty="children" onclick="nodeClick(this);"/>
</body>
</html>