EXT 异步树使用

假设,最后在前台要载现的树的效果如:
// c
// --c1
// --c4
// --c5
// --c6
// --c2
// --c7
// --c3
// --c8


1.前台JS,treePanelTest.js
//定义一个异步根节点的树
Ext.onReady(function(){
var urlTreeNode = new Ext.tree.AsyncTreeNode({
text:"c",
id:"c"
});
//定义树的加载方式
var urlTreeLoader = new Ext.tree.TreeLoader({
url:"../saleInfo.do?method=qryChildNode"
});
//定义加载时附加的参数
urlTreeLoader.on("beforeload",function(tl,node){
tl.baseParams.parentId = node.attributes.id;
},this);
//开始定义要展现的树的信息
var urlTree = new Ext.tree.TreePanel({
renderTo:"urlTreePanel",
root:urlTreeNode,
width:150,
loader:urlTreeLoader
});
//处现树的其它事件,比如点击后要处理的事件
urlTree.on("click",function(node,e){
//use the url do something
console.debug(node.attributes.url);
});
})


2.前台HTML.主要是定义一个层用来加载树,当然其它的ext js要引用进来

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>树面板</title>
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<link rel="stylesheet" type="text/css" href="../pubjs/ext/resources/css/ext-all.css">
<script type="text/javascript" src="../pubjs/ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../pubjs/ext/ext-all-debug.js"></script>
<script type="text/javascript" src="./treePanelTest.js"></script>

</head>
<body>

<div id="urlTreePanel"></div>
</body>
</html>
3.后台action,主要是取树的子节点的方法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.lin.web.common.JsonUtil;
import com.lins.j2ee.form.SaleInfoForm;
import com.lins.j2ee.mode.Order;
import com.lins.j2ee.mode.StuInfo;

public class BusiAction extends DispatchAction {
private CategoryService categoryService = new CategoryService();
public ActionForward qryChildNode(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String id = request.getParameter("parentId");
PrintWriter out = response.getWriter();
try{
Set set = categoryService.qryChildCategory(id);
System.out.println(set);
out.write(JsonUtil.getJsonString(set));
}catch(Exception ex){
ex.printStackTrace();

}finally{
out.flush();
out.close();
}
return null;
}

}
}

4.后台service


public class CategoryService {
private Category c;
public CategoryService(){
//初始化目录,增加信息
// c
// --c1
// --c4
// --c5
// --c6
// --c2
// --c7
// --c3
// --c8
//root
c = new Category();
c.setText("c");
c.setId("c");
//first level
Category c1 = new Category();
c1.setText("c1");
c1.setId("c1");
// c1.setParentCategory(c);
Category c2 = new Category();
c2.setText("c2");
c2.setId("c2");
// c2.setParentCategory(c);
Category c3 = new Category();
c3.setText("c3");
c3.setId("c3");
// c3.setParentCategory(c);
//add first level
c.setChildCategory(new HashSet());
c.getChildCategory().add(c1);
c.getChildCategory().add(c2);
c.getChildCategory().add(c3);

//second level
Category c4 = new Category();
c4.setText("c4");
c4.setId("c4");
c4.setUrl("http://www.163.com");
c4.setLeaf(true);
// c4.setParentCategory(c1);

Category c5 = new Category();
c5.setText("c5");
c5.setId("c5");
// c5.setParentCategory(c1);

Category c7 = new Category();
c7.setText("c7");
c7.setId("c7");
c7.setUrl("http://www.163.com");
c7.setLeaf(true);
// c7.setParentCategory(c2);

Category c8 = new Category();
c8.setText("c8");
c8.setId("c8");
c8.setUrl("http://www.163.com");
c8.setLeaf(true);
// c8.setParentCategory(c3);

//add second level
c1.setChildCategory(new HashSet());
c1.getChildCategory().add(c4);
c1.getChildCategory().add(c5);

c2.setChildCategory(new HashSet());
c2.getChildCategory().add(c7);

c3.setChildCategory(new HashSet());
c3.getChildCategory().add(c8);

//third level
Category c6 = new Category();
c6.setText("c6");
c6.setId("c6");
c6.setUrl("http://www.163.com");
c6.setLeaf(true);
// c6.setParentCategory(c5);

//add third level
c5.setChildCategory(new HashSet());
c5.getChildCategory().add(c6);

}
//查找ID值对应的父节点
public Category qryParentCategory(String id){
Category parentCategory = null;
if(c.getId().equals(id)){
return null;
}else{
return findParentCategory(c,id);
}
}
//查找本身节点
public Category qryCategory(String id){
Category category = null;
if(c.getId().equals(id)){
return c;
}else{
return findCategoryById(c,id);
}
}
//查找子节点
public Set qryChildCategory(String id){
//查找节点
Category c = qryCategory(id);
//查找子节点
if(c != null && c.getChildCategory() != null){
return c.getChildCategory();
}
return null;
}


private Category findParentCategory(Category category,String id){
Category parentCategory = null;
if(category.getId().equals(id)){
parentCategory = category.getParentCategory();
}else if(category.getChildCategory() != null){
for(Iterator iter = category.getChildCategory().iterator();iter.hasNext();){
Category c = (Category)iter.next();
Category tmpC = findParentCategory(c,id);
if(tmpC != null){
parentCategory = tmpC;
break;
}
}
}
return parentCategory;
}

private Category findCategoryById(Category category,String id){
Category curCategory = null;
if(category.getId().equals(id)){
return category;
}else{
if(category.getChildCategory() != null){
for(Iterator iter = category.getChildCategory().iterator();iter.hasNext();){
Category tmpCategory = (Category)iter.next();
curCategory = findCategoryById(tmpCategory,id);
if(curCategory != null){
break;
}
}
}
}
return curCategory;
}


//
// public Set qryChildrenCategory(String id){
//
// }



public static void main(String[] args){
CategoryService cs = new CategoryService();
Set s = cs.qryChildCategory("c");
System.out.println(s);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值