FunSettingTree.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CommonTool
  7. {
  8. public class FunSettingTree
  9. {
  10. public static FunSettingTree Inatance = new FunSettingTree();
  11. public FunctionTree CreatFunctionTree(List<FunctionTree> pcFunctionTrees)
  12. {
  13. FunctionTree loRoot = null;
  14. var lcFunctionTrees = pcFunctionTrees.Where(a => a.FatherId == "0").ToList();
  15. if (lcFunctionTrees.Count() == 1)
  16. {
  17. loRoot = lcFunctionTrees.FirstOrDefault();
  18. if (loRoot != null)
  19. {
  20. loRoot.ChildNodes = pcFunctionTrees.Where(a => a.FatherId == loRoot.Id && a.FatherId != "0").ToList();
  21. if (loRoot.ChildNodes.Any())
  22. {
  23. foreach (FunctionTree tree in loRoot.ChildNodes)
  24. {
  25. CreatFunctionTree(pcFunctionTrees, tree.Id);
  26. }
  27. }
  28. }
  29. }
  30. return loRoot;
  31. }
  32. public FunctionTree CreatFunctionTree(List<FunctionTree> pcFunctionTrees, string id)
  33. {
  34. FunctionTree loRoot = null;
  35. var lcFunctionTrees = pcFunctionTrees.Where(a => a.Id == id && a.FatherId != "0").ToList();
  36. if (lcFunctionTrees.Count() == 1)
  37. {
  38. loRoot = lcFunctionTrees.FirstOrDefault();
  39. if (loRoot != null)
  40. {
  41. loRoot.ChildNodes = pcFunctionTrees.Where(a => a.FatherId == loRoot.Id && a.FatherId != "0").ToList();
  42. if (loRoot.ChildNodes.Any())
  43. {
  44. foreach (FunctionTree tree in loRoot.ChildNodes)
  45. {
  46. CreatFunctionTree(pcFunctionTrees, tree.Id);
  47. }
  48. }
  49. }
  50. }
  51. return loRoot;
  52. }
  53. }
  54. public class FunctionTree
  55. {
  56. public string Id { get; set; }
  57. public string FatherId { get; set; }
  58. public string FunctionName { get; set; }
  59. public string ActionName { get; set; }
  60. public string ControllerName { get; set; }
  61. public string Url { get; set; }
  62. //public int Depth { get; set; }
  63. //public string IsLeaf { get; set; }
  64. //public int Sort { get; set; }
  65. public List<FunctionTree> ChildNodes { get; set; }
  66. public string ToJason()
  67. {
  68. StringBuilder sb = new StringBuilder();
  69. if (ChildNodes.Count > 0)
  70. {
  71. if (FatherId != "0")
  72. {
  73. sb.AppendFormat(
  74. "<li><a href = \"#\" ><i class=\"fa fa-clone\"></i><span class=\"nav-label\">{0}</span></a><ul class=\"nav nav-second-level\">",
  75. FunctionName);
  76. foreach (FunctionTree loChild in ChildNodes)
  77. {
  78. sb.Append(loChild.ToJason());
  79. }
  80. sb.AppendFormat("</ul></li>");
  81. }
  82. else
  83. {
  84. foreach (FunctionTree loChild in ChildNodes)
  85. {
  86. sb.Append(loChild.ToJason());
  87. }
  88. }
  89. }
  90. else
  91. {
  92. if (string.IsNullOrEmpty(ActionName))
  93. Url = "#";
  94. sb.AppendFormat("<li><a class=\"J_menuItem\" href=\"{0}\">{1}</a></li>", Url, FunctionName);
  95. }
  96. return sb.ToString();
  97. }
  98. //public string ToJbson()
  99. //{
  100. // StringBuilder sb = new StringBuilder();
  101. // if (ChildNodes != null)
  102. // {
  103. // foreach (FunctionTree loChild in ChildNodes)
  104. // {
  105. // sb.Append(loChild.ToJason());
  106. // }
  107. // }
  108. // else
  109. // {
  110. // if (string.IsNullOrEmpty(ActionName))
  111. // Url = "#";
  112. // sb.AppendFormat("<li><a class=\"J_menuItem\" href=\"{0}\">{1}</a></li>", Url, FunctionName);
  113. // }
  114. // return sb.ToString();
  115. //}
  116. }
  117. }