如何来做一棵递归树,这在前面的文章中有提过,但那是引用了myfaces组件的,今天就不用啥组件了,直接输出树的结构形状.
数据库表数据一样,但还是在此重复一次。
我们的目标是:完成以下形态的树状.
--管理
---高级管理员
----增加
----删除
----修改
---普通管理员
----增加
一、建表
/**
--树形菜单
节点编号
节点名称
节点链接址
些节点父节点
是否最底节点
节点排序(一般用在同级节点上)
节点说明
*/
drop table EPTreeTable
create table EPTreeTable
(
TreeNodeId int identity(1,1) constraint pk_TreeNode primary key,
TreeNodeName varchar(50) not null,
TreeNodeLink varchar(100) ,
TreeNodeFatherId int default 0, --0 为最高层
TreeNodeIsBottom bit default 1, --1 为底层 0为非底层
TreeNodeCompositor int default 1, -- 默认排在最后面
TreeNodeExplain varchar(200)
)
二、JAVA代码
(1)TO对象
/**
* @author fangbiao
* 用于映射父级菜单TO对象
*/
public class ParentResourcePojo {
/**
* 节点编号
*/
private int treeNodeId;
/**
* 节点名称
*/
private String treeNodeName;
/**
* 父节点
*/
private int treeParentNodeId;
/**
* @return treeNodeName
*/
public String getTreeNodeName() {
return treeNodeName;
}
/**
* @param treeNodeName 要设置的 treeNodeName
*/
public void setTreeNodeName(String treeNodeName) {
this.treeNodeName = treeNodeName;
}
/**
* @return treeParentNodeId
*/
public int getTreeParentNodeId() {
return treeParentNodeId;
}
/**
* @param treeParentNodeId 要设置的 treeParentNodeId
*/
public void setTreeParentNodeId(int treeParentNodeId) {
this.treeParentNodeId = treeParentNodeId;
}
/**
* @return treeNodeId
*/
public int getTreeNodeId() {
return treeNodeId;
}
/**
* @param treeNodeId 要设置的 treeNodeId
*/
public void setTreeNodeId(int treeNodeId) {
this.treeNodeId = treeNodeId;
}
}
(2)树形组合
/**
* 获取节点选择列表信息
* @return
*/
public List getResourceList(){
if(resourceList.size() == 0) {
//获取高级的父节点
resourceList = doTurnParentToTree(itbs.getTopParentResource());
}
return resourceList;
}
private int num =2;
private int tempTotalChildren ;
private boolean temp = false;
private int tempParent; //每次递归节点的父节点
/**
* 对父级列表进行树排
* @param prp
* @return
*/
private synchronized List doTurnParentToTree(ParentResourcePojo prp){
if(prp == null)
return null;
List list = getChildrenList(prp);
//分线
tempParent = prp.getTreeParentNodeId();
String str = "";
if(tempParent == 0){
resourceList.add("-"+prp.getTreeNodeName()));
}else{
resourceList.add(prp.getTreeNodeName()));
//每棵树的节点数目
tempTotalChildren = list.size();
}
Iterator iterator = list.iterator();
while(iterator.hasNext()){
ParentResourcePojo treePojo = (ParentResourcePojo)iterator.next();
if(tempParent != 0){
if( tempParent < treePojo.getTreeParentNodeId()){
if(temp == false)
num += 1;
}else if( tempParent > treePojo.getTreeParentNodeId()){
num -=1;
temp = false;
}else if( tempParent == treePojo.getTreeParentNodeId() ){
if(tempTotalChildren ==0)
temp = false;
else
temp = true;
}
}
for(int i=0;i<num;i++){
str = str + "-";
}
treePojo.setTreeNodeName(CommonUtil.doAddAppendString(str,treePojo.getTreeNodeName()));
str = "";
doTurnParentToTree(treePojo);
}
return resourceList;
}
/**
* 父级菜单列表
*/
private List allParentTreeList = null;
/**
* 做标记用
*/
private int flag = 0;
/**
* 获取所有父级菜单列表树
* @return
*/
private List getAllParentTreeList(){
if(allParentTreeList == null){
if(flag == 0){
flag = 1;
allParentTreeList = itbs.getRRCResource();
return allParentTreeList;
}
return allParentTreeList;
}
return allParentTreeList;
}
/**
* 取子树列
* @param prp
* @return
*/
private List getChildrenList(ParentResourcePojo prp){
List list = getAllParentTreeList();
int listLength = list.size();
List newReturnList = new ArrayList();
if(listLength == 0)
return newReturnList;
for(int i=0;i<listLength;i++){
ParentResourcePojo prPojo = (ParentResourcePojo)list.get(i);
int treePojoTreeParantNodeId = prPojo.getTreeParentNodeId();
int tpTreeNodeId = prp.getTreeNodeId();
if(treePojoTreeParantNodeId == tpTreeNodeId){
newReturnList.add(prPojo);
}else{
leaveList.add(prPojo);
}
}
list = leaveList;
return newReturnList;
}