update:treenode remove empty children

This commit is contained in:
2021-06-29 11:08:46 +08:00
parent a0c98b5014
commit 056445db89
2 changed files with 26 additions and 13 deletions

View File

@@ -27,6 +27,10 @@ namespace Ewide.Core
/// <param name="children"></param>
void SetChildren(IList children);
}
public class TreeBuildSetting
{
public bool AddEmptyChildren { get; set; } = true;
}
/// <summary>
/// 递归工具类,用于遍历有父子关系的节点,例如菜单树,字典树等等
@@ -34,10 +38,15 @@ namespace Ewide.Core
/// <typeparam name="T"></typeparam>
public class TreeBuildUtil<T> where T : ITreeNode
{
TreeBuildSetting _setting;
public TreeBuildUtil(TreeBuildSetting setting = null)
{
_setting = setting ?? new TreeBuildSetting();
}
/// <summary>
/// 顶级节点的父节点Id(默认0)
/// </summary>
private readonly List<string> _rootParentIds = new List<string> {string.Empty,Guid.Empty.ToString() };
private readonly List<string> _rootParentIds = new List<string> { string.Empty, Guid.Empty.ToString() };
/// <summary>
/// 构造树节点
@@ -59,7 +68,8 @@ namespace Ewide.Core
if (results.Count < 1)
{
var ids = new List<string>();
nodes.ForEach(u => {
nodes.ForEach(u =>
{
ids.Add(u.GetId());
});
@@ -89,7 +99,10 @@ namespace Ewide.Core
});
nodeSubLists.ForEach(u => BuildChildNodes(totalNodes, u, new List<T>()));
childNodeLists.AddRange(nodeSubLists);
node.SetChildren(childNodeLists);
if (childNodeLists.Count > 0 || _setting.AddEmptyChildren)
{
node.SetChildren(childNodeLists);
}
}
}
}