BootStrapTree.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace CommonTool
  4. {
  5. public class BootStrapTree
  6. {
  7. public static BootStrapTree Inatance = new BootStrapTree();
  8. public Tree CreatTree(List<Tree> pcTrees)
  9. {
  10. Tree loRoot = null;
  11. var lcTrees = pcTrees.Where(a => a.FatherId == "0").ToList();
  12. if (lcTrees.Count() == 1)
  13. {
  14. loRoot = lcTrees.FirstOrDefault();
  15. if (loRoot != null)
  16. {
  17. loRoot.ChildNodes = pcTrees.Where(a => a.FatherId == loRoot.Id && a.FatherId != "0").ToList();
  18. if (loRoot.ChildNodes.Any())
  19. {
  20. foreach (Tree tree in loRoot.ChildNodes)
  21. {
  22. CreatTree(pcTrees, tree.Id);
  23. }
  24. }
  25. }
  26. }
  27. return loRoot;
  28. }
  29. public Tree CreatTree(List<Tree> pcTrees,string id)
  30. {
  31. Tree loRoot = null;
  32. var lcTrees = pcTrees.Where(a => a.Id == id && a.FatherId != "0").ToList();
  33. if (lcTrees.Count() == 1)
  34. {
  35. loRoot = lcTrees.FirstOrDefault();
  36. if (loRoot != null)
  37. {
  38. loRoot.ChildNodes = pcTrees.Where(a => a.FatherId == loRoot.Id && a.FatherId != "0").ToList();
  39. if (loRoot.ChildNodes.Any())
  40. {
  41. foreach (Tree tree in loRoot.ChildNodes)
  42. {
  43. CreatTree(pcTrees,tree.Id);
  44. }
  45. }
  46. }
  47. }
  48. return loRoot;
  49. }
  50. }
  51. public class Tree
  52. {
  53. //public Tree(IQueryable<Tree> trees)
  54. //{
  55. // Trees = trees;
  56. //}
  57. //public Tree newTree = BootStrapTree.Inatance.CreatTree(Trees);
  58. //public static IQueryable<Tree> Trees { get; set; }
  59. public string Id { get; set; }
  60. public string Text { get; set; }
  61. public string FatherId { get; set; }
  62. //public int Depth { get; set; }
  63. //public string IsLeaf { get; set; }
  64. //public int Sort { get; set; }
  65. public List<Tree> ChildNodes { get; set; }
  66. public string ToJason()
  67. {
  68. string lcRetVal = "{ \r\n";
  69. lcRetVal += "\"id\": \"" + Id + "\",\r\n";
  70. lcRetVal += "\"text\": \"" + Text+ "\"";
  71. if (ChildNodes != null)
  72. {
  73. lcRetVal += ",\r\n\"nodes\": [";
  74. string lcString = "";
  75. foreach (Tree loChild in ChildNodes)
  76. {
  77. lcString += (lcString == "" ? "" : ",") + loChild.ToJason();
  78. }
  79. lcRetVal += lcString;
  80. lcRetVal += "] \r\n";
  81. }
  82. lcRetVal += "} \r\n";
  83. return lcRetVal;
  84. }
  85. public string ToJbson()
  86. {
  87. return "[ "+ ToJason()+"]";
  88. }
  89. }
  90. }