java非常好用的树形结构容易拓展,附代码,6个类

在这里插入图片描述
分组的实体TDataGroup类就不写了,强调一点要有parentId,加上一个sort最好
controller-----------------------------------------------------------------------------------------------
@RequestMapping(value = “/tree.json”, method = RequestMethod.POST)
@ApiOperation(value = “获取分组树形结构”, notes = “获取分组树形结构”)
public JsonResult<List> tree() {
List tree = tDataGroupService.getGroupTree();
return JsonResult.success(tree);
}

service-----------------------------------------------------------------------------------------------
public List getGroupTree() {
List list = tDataGroupDao.all();
TGroupItem root = GroupBuildUtil.buildOrgTree(list, 0L);
return buildGroupTree(root);

}

private List<GroupNodeView> buildGroupTree(TGroupItem node) {
    List<TGroupItem> list = node.getChildren();
    if (list.size() == 0) {
        return Collections.EMPTY_LIST;
    }
    List<GroupNodeView> views = new ArrayList<GroupNodeView>(list.size());
    for (TGroupItem item : list) {
        GroupNodeView view = new GroupNodeView();
        view.setGroupName(item.getData().getGroupName());
        view.setGroupDesc(item.getData().getGroupDesc());
        view.setSort(Long.valueOf(item.getData().getSort()));
        view.setModifyUser(Integer.valueOf(item.getData().getModifyUser().toString()));
        view.setId(item.getData().getId());
        List<GroupNodeView> children = this.buildGroupTree(item);
        if (CollectionUtils.isNotEmpty(children)) {
            Map<String, Object> addProperties = new HashMap() {{
                put("children", children);
            }};
            GroupNodeView viewTmp = (GroupNodeView) ReflectUtil.getTarget(view, addProperties);
            views.add(viewTmp);
        } else {
            views.add(view);
        }

    }
    return views;
}

节点视图实体类-----------------------------------------------------------------------------------------------

public class GroupNodeView {

private String groupName;
private String groupDesc;
private Long sort;
private Integer modifyUser;
private Long id;

// List children=new ArrayList();
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getGroupDesc() {
return groupDesc;
}
public void setGroupDesc(String groupDesc) {
this.groupDesc = groupDesc;
}
public Long getSort() {
return sort;
}
public void setSort(Long sort) {
this.sort = sort;
}
public Integer getModifyUser() {
return modifyUser;
}
public void setModifyUser(Integer modifyUser) {
this.modifyUser = modifyUser;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
// public List getChildren() {
// return children;
// }
// public void setChildren(List children) {
// this.children = children;
// }
@Override
public String toString() {
return “GroupNodeView [groupName=” + groupName + “, groupDesc=” + groupDesc + “, sort=” + sort + “, modifyUser=”
+ modifyUser + “, id=” + id + “, children=]”;
}
}

TGroupItem 类-----------------------------------------------------------------------------------------------
public class TGroupItem implements TreeItem{

TDataGroup sysGroup = null;
List<TGroupItem> children = new ArrayList<>();
@JsonIgnore
TGroupItem parent = null;
String groupDesc ;
public TGroupItem(TDataGroup sysGroup){
	this.sysGroup = sysGroup;
	this.groupDesc = sysGroup!=null?sysGroup.getGroupDesc():null;
}


public void setParent(TGroupItem parent){
	this.parent = parent;
	parent.children.add(this);
}

public List<TGroupItem> getChildren(){
	return this.children;
}

public Long getId(){
	return sysGroup.getId();
}

public TDataGroup getData(){
	return this.sysGroup;
}

/**
 * 查找某个指定的子节点
 * @param functionId
 * @return
 */
public TGroupItem findChild(long functionId){
	if(sysGroup.getId()==functionId){
		return this;
	}
	for(TGroupItem item:children){
		TGroupItem find = item.findChild(functionId);
		if(find!=null){
			return find;
		}
	}
	return null;		
}

/**
 * 查找所有的子节点
 * @return
 */
public List<TGroupItem> findAllItem(){
	List<TGroupItem> all = new LinkedList<>();
	findAllChildItem(all,this);
	return all;
}

public List<Long> findAllChildrenId(){
	List<TGroupItem> items =findAllItem();
	List<Long> children = new ArrayList<Long>();
	for(TGroupItem item:items){
		children.add(item.getId());
	}
	return children;
}
private void findAllChildItem(List<TGroupItem> all,TGroupItem parent){
	for(TGroupItem item:parent.children){
		all.add(item);
		findAllChildItem(all,item);
	}
}
@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((sysGroup == null) ? 0 : sysGroup.hashCode());
	return result;
}
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	TGroupItem other = (TGroupItem) obj;
	if (sysGroup == null) {
		if (other.sysGroup != null)
			return false;
	} else if (!sysGroup.equals(other.sysGroup))
		return false;
	return true;
}
@Override
public String toString() {
	return "MenuItem [sysGroup=" + sysGroup.getGroupName() + ","+this.children.size()+"]";
}
@Override
public String getName() {
	return sysGroup==null?"":sysGroup.getGroupName();
}

}

接口TreeItem -----------------------------------------------------------------------------------------------
public interface TreeItem extends java.io.Serializable {
public String getName();
public Long getId();
public List getChildren();
}

$(function(){ $.fn.extend({ SimpleTree:function(options){ //初始化参数 var option = $.extend({ click:function(a){ } },options); option.tree=this; /* 在参数对象中添加对当前菜单树的引用,以便在对象中使用该菜单树 */ option._init=function(){ /* * 初始化菜单展开状态,以及分叉节点的样式 */ this.tree.find("ul ul").hide(); /* 隐藏所有子级菜单 */ this.tree.find("ul ul").prev("li").removeClass("open"); /* 移除所有子级菜单父节点的 open 样式 */ this.tree.find("ul ul[show='true']").show(); /* 显示 show 属性为 true 的子级菜单 */ this.tree.find("ul ul[show='true']").prev("li").addClass("open"); /* 添加 show 属性为 true 的子级菜单父节点的 open 样式 */ }/* option._init() End */ /* 设置所有超链接不响应单击事件 */ this.find("a").click(function(){ $(this).parent("li").click(); return false; }); /* 菜单项 接受单击 */ this.find("li").click(function(){ /* * 当单击菜单项 * 1.触发用户自定义的单击事件,将该 标签中的第一个超链接做为参数传递过去 * 2.修改当前菜单项所属的子菜单的显示状态(如果等于 true 将其设置为 false,否则将其设置为 true) * 3.重新初始化菜单 */ option.click($(this).find("a")[0]); /* 触发单击 */ /* * 如果当前节点下面包含子菜单,并且其 show 属性的值为 true,则修改其 show 属性为 false * 否则修改其 show 属性为 true */ /* if($(this).next("ul").attr("show")=="true"){ $(this).next("ul").attr("show","false"); }else{ $(this).next("ul").attr("show","true"); }*/ /* 初始化菜单 */ option._init(); }); /* 设置所有父节点样式 */ this.find("ul").prev("li").addClass("folder"); /* 设置节点“是否包含子节点”属性 */ this.find("li").find("a").attr("hasChild",false); this.find("ul").prev("li").find("a").attr("hasChild",true); /* 初始化菜单 */ option._init(); }/* SimpleTree Function End */ }); });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值