public class DepartmentUtils {
/**
* @param topList *部门列表
* @param removeId 删除部门的id
* @return
*/
public static List<Department> getTreeList(List<Department> topList,Long removeId) {
List<Department> treeList = new ArrayList<Department>();
walkTree(topList,treeList,"┣",removeId);
return treeList;
}
/**
* 组织树形部门数据
*/
public static void walkTree(Collection<Department> topList,List<Department> treeList ,String prefix ,Long removeId){
for(Department d : topList){
if(removeId != null && d.getId().equals(removeId)){
//结束本次循环,直接下一次循环
continue;
}
//为了避免修改数据库,创建出新的对象,修改新的对象,在放入到treeList里面
Department copy = new Department();
copy.setId(d.getId());
copy.setName(prefix + d.getName());
//顶点
treeList.add(copy);
//子树
Set<Department> children = d.getChildren();
walkTree(children,treeList," " + prefix,removeId);
}
}
}
在部门action里面的调用:
public String editUI(){
//准备数据:要修改的部门
Department dept = departmentService.getById(model.getId());
//准备数据:部门列表
List<Department> topList = departmentService.findTopList();//所有*部门列表
List<Department> treeList = DepartmentUtils.getTreeList(topList,dept.getId());
getValueStack().set("departmentList", treeList);
getValueStack().push(dept);
if(dept.getParent() != null){
parentId = dept.getParent().getId();//设置parentId的值,用于回显
}
return "editUI";
}