/// <summary> /// 分類樹形結構 http://192.168.2.177:1222/api/classification/GetclassificationTree /// </summary> /// <returns></returns> [Route("api/classification/GetclassificationTree")] [HttpGet] public string GetclassificationTree() { WebApplication.Controllers.TreeMethod tm = new TreeMethod(); // 找到所有的父節點 List<TreeEntity> treeList1 = tm.findAllParents(); if (treeList1 != null) { for (int i = 0; i < treeList1.Count; i++) { TreeEntity tree = treeList1[i]; // 拼接父節點 //result += "|--" + tree.name; result += "{id:"+tree.id+ ",pId:"+tree.pid+",name:'"+tree.name+"'},"; // 綁定孩子 ChildResult = tm.BindChildByParent(tree.id, "").TrimEnd(','); temp = result + ChildResult; } } else { temp += "沒有數據!"; } result = "["+ temp.TrimEnd(',')+ "]"; return SqlSugar.JsonConverter.Serialize(new { Result = 1, Msg = "獲取數據成功",Data= temp }); } } public class TreeEntity { public string id { get; set; } public string name { get; set; } public string pid { get; set; } } internal class TreeMethod { /// <summary> /// 找到所有的父節點 /// </summary> /// <returns></returns> public List<TreeEntity> findAllParents() { List<TreeEntity> treeList = new List<TreeEntity>(); using (var db = SugarDao.GetInstance()) { var list = db.Queryable<tb_classification>().Where(it => it.pid == 0 && it.status==1).ToList(); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { TreeEntity myTree = new TreeEntity(); myTree.id = list[i].id.ToString(); myTree.name = list[i].title; myTree.pid = list[i].pid.ToString(); treeList.Add(myTree); } } } return treeList; } /// <summary> /// 根據父節點找到所有的子節點 /// </summary> /// <param name="pid"></param> /// <returns></returns> public List<TreeEntity> findChildByPid(string pid) { int p_id = Convert.ToInt32(pid); List<TreeEntity> treeList = new List<TreeEntity>(); using (var db = SugarDao.GetInstance()) { var list = db.Queryable<tb_classification>().Where(it => it.pid == p_id&&it.status==1).ToList(); if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { TreeEntity myTree = new TreeEntity(); myTree.id = list[i].id.ToString(); myTree.name = list[i].title; myTree.pid = list[i].pid.ToString(); treeList.Add(myTree); } } } return treeList; } /// <summary> /// 查看是否存在子節點 /// </summary> /// <param name="pid"></param> /// <returns></returns> public bool HasChild(string pid) { int p_id = Convert.ToInt32(pid); int count = 0; bool flag = false; using (var db = SugarDao.GetInstance()) { var list = db.Queryable<tb_classification>().Where(it => it.pid == p_id&it.status==1).ToList(); for (int i = 0; i < list.Count; i++) { count++; } if (count > 0) { flag = true; } } return flag; } string Tree = string.Empty; /// <summary> /// 使用遞歸拼接父節點的子節點 /// </summary> /// <param name="pid"></param> /// <param name="prefix"></param> public string BindChildByParent(string pid, string prefix) { if (this.HasChild(pid)) { // 得到當前父節點下的所有孩子 List<TreeEntity> list = this.findChildByPid(pid); // 循環拼接當前父節點下的孩子 for (int i = 0; i < list.Count; i++) { //Tree += "|----" + prefix + list[i].name; Tree += "{id:" + list[i].id + ",pId:" + list[i].pid + ",name:'" + list[i].name + "'},"; if (this.HasChild(list[i].id)) { this.BindChildByParent(list[i].id, "--"); } } } return Tree; } }
最終效果:
"{\"Result\":1,\"Msg\":\"獲取數據成功\",\"Data\":\"{id:1,pId:0,name:\\u0027新聞\\u0027},{id:16,pId:0,name:\\u0027蔬菜\\u0027},{id:2,pId:1,name:\\u0027娛樂新聞\\u0027},{id:20,pId:2,name:\\u0027新增新聞分類\\u0027},{id:23,pId:20,name:\\u0027新增新聞分類2\\u0027},{id:26,pId:23,name:\\u0027新增分類3\\u0027},{id:7,pId:1,name:\\u0027時政新聞\\u0027},{id:8,pId:1,name:\\u0027科教文衛\\u0027},{id:9,pId:1,name:\\u0027城建公交\\u0027},{id:10,pId:1,name:\\u0027經濟新聞\\u0027},{id:11,pId:1,name:\\u0027政法新聞\\u0027},{id:12,pId:1,name:\\u0027社會新聞\\u0027},{id:13,pId:1,name:\\u0027體育新聞\\u0027},{id:14,pId:1,name:\\u0027軍事新聞\\u0027},{id:15,pId:1,name:\\u0027國際新聞\\u0027},{id:18,pId:16,name:\\u0027白菜\\u0027},{id:19,pId:16,name:\\u0027蘿卜\\u0027},\"}"