CheckListTreeExtensions.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. namespace Bowin.Web.Controls.Mvc
  7. {
  8. public static class CheckListTreeExtensions
  9. {
  10. public static MvcHtmlString CheckListTree(this HtmlHelper htmlHelper,
  11. ListTreeOptions listTreeOptions)
  12. {
  13. var topItems = listTreeOptions.ItemList.Where(x => x.ParentValue == null).ToList();
  14. if (listTreeOptions == null)
  15. {
  16. listTreeOptions = new ListTreeOptions();
  17. }
  18. if((listTreeOptions.ItemName ?? "") == "")
  19. {
  20. listTreeOptions.ItemName = "checkBoxTreeList" + Guid.NewGuid().ToString();
  21. }
  22. if ((listTreeOptions.LeafWidth ?? 0) < 1)
  23. {
  24. listTreeOptions.LeafWidth = 1;
  25. }
  26. StringBuilder html = new StringBuilder();
  27. for (int i = 0; i < topItems.Count; i ++ )
  28. {
  29. string myID = listTreeOptions.ItemName +Guid.NewGuid()+ i;
  30. int level = 0;
  31. html.AppendFormat("<div><input id='{4}' onclick='checkBranch();' type='checkbox' name='{2}'{3} value='{0}'/>{1}</div>",
  32. topItems[i].Value,
  33. topItems[i].Text,
  34. listTreeOptions.ItemName,
  35. ((listTreeOptions.SelectedValues.Contains(topItems[i].Value)) ? " checked" : ""),
  36. myID);
  37. AddSubItems(html, listTreeOptions, myID, topItems[i], level);
  38. }
  39. return MvcHtmlString.Create(html.ToString());
  40. }
  41. private static void AddSubItems(StringBuilder html, ListTreeOptions listTreeOptions, string parentID, ListTreeItem item, int level)
  42. {
  43. var curItems = listTreeOptions.ItemList.Where(x => (x.ParentValue != null) ? x.ParentValue.Equals(item.Value) : false);
  44. var leafItems = curItems.Where(x => !listTreeOptions.ItemList.Exists(y => (y.ParentValue != null) ? y.ParentValue.Equals(x.Value) : false)).ToList();
  45. var branchItems = curItems.Where(x => !leafItems.Contains(x)).ToList();
  46. var nowLevel = level + 1;
  47. int rowCount = Convert.ToInt32(Math.Ceiling(((double)leafItems.Count()) / listTreeOptions.LeafWidth.Value));
  48. for (int i = 0; i < rowCount; i ++)
  49. {
  50. html.Append("<div>");
  51. for (int k = 0; k < nowLevel; k++)
  52. {
  53. html.Append("&nbsp;&nbsp;");
  54. }
  55. for (int j = 0; j < listTreeOptions.LeafWidth; j++)
  56. {
  57. var curIndex = i * listTreeOptions.LeafWidth.Value + j;
  58. if (leafItems.Count > curIndex)
  59. {
  60. string myID = parentID + "l" + curIndex;
  61. html.AppendFormat("<input id='{4}' type='checkbox' name='{2}'{3} value='{0}'/>{1}",
  62. leafItems[curIndex].Value,
  63. leafItems[curIndex].Text,
  64. listTreeOptions.ItemName,
  65. ((listTreeOptions.SelectedValues.Contains(leafItems[curIndex].Value)) ? " checked" : ""),
  66. myID);
  67. }
  68. html.Append("&nbsp;");
  69. }
  70. html.Append("</div>");
  71. }
  72. for (int i = 0; i < branchItems.Count; i ++ )
  73. {
  74. string myID = parentID + i;
  75. html.Append("<div>");
  76. for (int j = 0; j < nowLevel; j++)
  77. {
  78. html.Append("&nbsp;&nbsp;");
  79. }
  80. html.AppendFormat("<input id='{4}' onclick='checkBranch();' type='checkbox' name='{2}'{3} value='{0}'/>{1}",
  81. branchItems[i].Value,
  82. branchItems[i].Text,
  83. listTreeOptions.ItemName,
  84. ((listTreeOptions.SelectedValues.Contains(branchItems[i].Value)) ? " checked" : ""),
  85. myID);
  86. html.Append("</div>");
  87. AddSubItems(html, listTreeOptions, myID, branchItems[i], nowLevel);
  88. }
  89. }
  90. }
  91. }