1、ztree用于前台树形结构显示需要json数据
基础的树形对象:
import java.io.Serializable;
import java.util.List;
/**
* treegrid树形表格基础对象,后续的该类型的对象均继承该对象
*
*/
public class BaseTreeGrid implements Serializable{
/**
*
*/
private static final long serialVersionUID = -9189631784252440402L;
public String id;//节点id
public String parentId;//节点父id
public String iconCls = "folder";//节点样式,默认即可
public Boolean leaf = true;//是否为叶子节点,true表示是叶子节点,false表示不是叶子节点
public Boolean expanded = true; //是否展开,默认true,展开
public Listchildren;//孩子节点
public BaseTreeGrid() {
}
public BaseTreeGrid(String id, String parentId) {
this.id=id;
this.parentId=parentId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getIconCls() {
return iconCls;
}
public void setIconCls(String iconCls) {
this.iconCls = iconCls;
}
public Boolean getLeaf() {
return leaf;
}
public void setLeaf(Boolean leaf) {
this.leaf = leaf;
}
public Boolean getExpanded() {
return expanded;
}
public void setExpanded(Boolean expanded) {
this.expanded = expanded;
}
public ListgetChildren() {
return children;
}
public void setChildren(Listchildren) {
this.children = children;
}
}
2、测试类
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.eshore.common.ext.BaseTreeGrid;
/**
* 将记录list转化为树形list
* 基于BaseTreeGid类的转换
*
*/
public class TreeUtils {
/**
* 格式化list为树形list
* @param list
* @param falg true 表示全部展开,其他 表示不展开
* @return
*/
public static ListformatTree(Listlist, Boolean flag) {
ListnodeList = new ArrayList();
for(T node1 : list){
boolean mark = false;
for(T node2 : list){
if(node1.getParentId()!=null && node1.getParentId().equals(node2.getId())){
node2.setLeaf(false);
mark = true;
if(node2.getChildren() == null) {
node2.setChildren(new ArrayList());
}
node2.getChildren().add(node1);
if (flag) {
//默认已经全部展开
} else{
node2.setExpanded(false);
}
break;
}
}
if(!mark){
nodeList.add(node1);
if (flag) {
//默认已经全部展开
} else{
node1.setExpanded(false);
}
}
}
return nodeList;
}
public static void main(String[] args) {
Listlist = new ArrayList();
BaseTreeGrid root1 = new BaseTreeGrid();
root1.setId("1");
BaseTreeGrid child1 = new BaseTreeGrid();
child1.setId("11");
child1.setParentId("1");
BaseTreeGrid child11 = new BaseTreeGrid();
child11.setId("111");
child11.setParentId("11");
BaseTreeGrid root2 = new BaseTreeGrid();
root2.setId("2");
BaseTreeGrid child2 = new BaseTreeGrid();
child2.setId("21");
child2.setParentId("2");
list.add(root1);
list.add(child1);
list.add(child11);
list.add(root2);
list.add(child2);
Listtreelist = formatTree(list, false);
String json = JSONArray.toJSONString(treelist);
System.out.println(json);
}
}
3、树结构 递归查询出给定节点的所有子孙节点
package com.ps.uzkefu.apps.knowledge.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ps.uzkefu.apps.knowledge.entity.QuickReply;
import com.ps.uzkefu.apps.knowledge.entity.QuickType;
import com.ps.uzkefu.apps.knowledge.mapper.QuickTypeDao;
import com.ps.uzkefu.apps.knowledge.service.QuickReplyService;
import com.ps.uzkefu.apps.knowledge.service.QuickTypeService;
import com.ps.uzkefu.base.BaseServiceImpl;
import com.ps.uzkefu.common.Result;
import com.ps.uzkefu.util.TreeNode;
import com.ps.uzkefu.util.TreeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
*
* 知识库快捷回复类型 服务实现类
*
*
* @author WuZhiWei
* @since 2018-07-06
*/
@Service
public class QuickTypeServiceImpl extends BaseServiceImplimplements QuickTypeService {
@Autowired
QuickReplyService quickReplyService;
@Override
public ListgetQuickTypeTree(QuickType quickType) {
EntityWrapperentityWrapper = new EntityWrapper<>();
if (StringUtils.isNotBlank(quickType.getId())){
entityWrapper.eq("parentid",quickType.getId());
}
entityWrapper.eq("corp_code",quickType.getCorpCode()).orderBy("order_num");
ListquickTypes = this.selectList(entityWrapper);
ListtreeNodes = new ArrayList<>();
TreeNode treeNode = null;
for (QuickType q:quickTypes) {
treeNode = new TreeNode();
treeNode.setpId(q.getParentid());
treeNode.setId(q.getId());
treeNode.setName(q.getName());
treeNode.setOpen("true");
treeNode.setOrderNum(q.getOrderNum());
treeNodes.add(treeNode);
}
Listtrees = TreeUtils.formatTree(treeNodes,true);
Collections.sort(trees,new Comparator() {
@Override
public int compare(TreeNode o1, TreeNode o2) {
// 返回值为int类型,大于0表示正序,小于0表示逆序
return o1.getOrderNum()-o2.getOrderNum();
}
});
return trees;
}
@Override
public ResultdeleteQuickTypeById(String typeid) {
EntityWrapperentityWrapper = new EntityWrapper<>();
entityWrapper.eq("id",typeid).or("parentid",typeid);
boolean result = this.delete(entityWrapper);
if (result){
EntityWrapperwrapper = new EntityWrapper<>();
// todo 递归删除
}else {
}
return null;
}
@Override
public ListgetAllParent(QuickType quickType) {
return this.baseMapper.selectAllParent(quickType.getCorpCode());
}
@Override
public ListgetAllChild(String typeid,String corpcode) {
QuickType quickType = new QuickType();
quickType.setCorpCode(corpcode);
ListquickTypes = new ArrayList<>();
quickTypes = getTreeList(quickTypes,typeid);
Listids = new ArrayList<>();
for (QuickType node:quickTypes) {
ids.add(node.getId());
}
return ids;
}
/**
* 递归查询给定节点id的所有子孙节点
* @param domainList
* @param pid
* @return
*/
@Override
public ListgetTreeList(ListdomainList ,String pid) {
//加入它本身
QuickType model = new QuickType();
model.setId(pid);
domainList.add(model);
//加入子集
QuickType domainType = new QuickType();
domainType.setParentid(pid);
Listlist = selectList(new EntityWrapper().eq("parentid",pid));
for(QuickType type : list) {
getTreeList(domainList,type.getId());
}
return domainList;
}
}