/// <summary> /// 生成树形 JSON 数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dataList">数据列表</param> /// <param name="maxLength">最大编号长度,为 0 程序自动生成</param> /// <param name="rootID">根节点 ID</param> /// <param name="jsonTemplate">Json 模板,例:{ id:1, name:"name" }</param> /// <returns></returns> public static string ToJson <T>(List <T> dataList, int maxLength = 0, string rootID = "0", ITreeJsonTemplate <T> jsonTemplate = null) where T : ITreeData { if (dataList == null || dataList.Count == 0) { return("{}"); } List <TreeNode <T> > nodeList = ToTree <T>(dataList, maxLength, rootID, jsonTemplate); if (nodeList == null || nodeList.Count == 0) { return("{}"); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("["); int index = 0; foreach (TreeNode <T> nodeItem in nodeList) { stringBuilder.Append(nodeItem.JsonText); if (index < nodeList.Count - 1) { stringBuilder.Append(","); } index++; } stringBuilder.Append("]"); return(stringBuilder.ToString()); }
/// <summary> /// 获取树形数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dataList">数据列表</param> /// <param name="maxLength">最大编号长度,大于 0 程序自动生成</param> /// <param name="rootID">根节点 ID</param> /// <param name="jsonTemplate">模板</param> /// <param name="layerNameSplitChar">分隔符,例:/</param> /// <param name="layerIDSplitChar">分隔符,例:/</param> /// <returns></returns> public static List <TreeNode <T> > ToTree <T>(List <T> dataList, int maxLength = 0, string rootID = "0", ITreeJsonTemplate <T> jsonTemplate = null, string layerNameSplitChar = "/", string layerIDSplitChar = ",") where T : ITreeData { #region 设置排序序号 if (maxLength > 0) { foreach (T t in dataList) { t.TreeUniqueIdentity = long.Parse(string.Format(TreeUniqueIdentityFormat, t.TreeParentID.PadLeft(maxLength, '0'), t.TreeID.PadLeft(maxLength, '0'))); } } #endregion #region 根据排序序号排序 dataList.Sort((x, y) => { return((int)(x.TreeUniqueIdentity - y.TreeUniqueIdentity)); }); #endregion TreeNode <T> treeNode = null; List <TreeNode <T> > nodeList = new List <TreeNode <T> >(); Dictionary <string, TreeNode <T> > nodeDict = new Dictionary <string, TreeNode <T> >(); foreach (T t in dataList) { // 设置根数据 if (t.TreeParentID == rootID) { treeNode = new TreeNode <T>() { Data = t, NodeList = new List <TreeNode <T> >(), LayerIndex = 1, IJsonTemplate = jsonTemplate, LayerID = t.TreeID, LayerName = t.TreeName }; nodeList.Add(treeNode); nodeDict.Add(t.TreeID, treeNode); } else { if (nodeDict.ContainsKey(t.TreeParentID)) { treeNode = new TreeNode <T>() { Data = t, NodeList = new List <TreeNode <T> >(), LayerIndex = nodeDict[t.TreeParentID].LayerIndex + 1, IJsonTemplate = jsonTemplate, LayerID = string.Format("{0}{1}{2}", nodeDict[t.TreeParentID].LayerID, layerIDSplitChar, t.TreeID), LayerName = string.Format("{0}{1}{2}", nodeDict[t.TreeParentID].LayerName, layerNameSplitChar, t.TreeName) }; treeNode.ParentNode = nodeDict[t.TreeParentID]; treeNode.ParentNode.NodeList.Add(treeNode); nodeDict.Add(t.TreeID, treeNode); } } } return(nodeList); }