Tree.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 class Tree : BaseControl
  9. {
  10. public string ItemSourceUrl { get; set; }
  11. public bool? IsCheckTree { get; set; }
  12. public UrlHelper UrlHelper { get; set; }
  13. public bool? IsCascadeCheck { get; set; }
  14. public override string Render()
  15. {
  16. StringBuilder sb = new StringBuilder();
  17. TagBuilder tabBuilder = new TagBuilder("ul");
  18. tabBuilder.MergeAttributes<string, string>(GetPropertyList());
  19. if (this.Attributes != null)
  20. {
  21. tabBuilder.MergeAttributes<string, string>(this.Attributes);
  22. }
  23. sb.Append(string.Format("<input type=hidden id={0} name={1}>", ID + "_hidden", Name));
  24. sb.Append(tabBuilder.ToString(TagRenderMode.Normal));
  25. return sb.ToString();
  26. }
  27. protected IDictionary<string, string> GetPropertyList()
  28. {
  29. var dictPropertyList = new Dictionary<string, string>();
  30. if (!string.IsNullOrEmpty(this.ID))
  31. {
  32. dictPropertyList.Add("id", this.ID);
  33. }
  34. dictPropertyList.Add("class", string.Format("easyui-tree {0}", (string.IsNullOrEmpty(CssClass) ? "" : CssClass)));
  35. var dictOptions = new Dictionary<string, string>();
  36. if (!string.IsNullOrEmpty(this.ItemSourceUrl))
  37. {
  38. dictOptions.Add("url", "'" + UrlHelper.Content(ItemSourceUrl) + "'");
  39. }
  40. else
  41. {
  42. throw new Exception("请输入获取树节点的数据源Url。");
  43. }
  44. if (this.IsCheckTree.HasValue && this.IsCheckTree.Value)
  45. {
  46. dictOptions.Add("checkbox", "true");
  47. }
  48. if (this.IsCascadeCheck.HasValue)
  49. {
  50. dictOptions.Add("cascadeCheck", this.IsCascadeCheck == true ? "true" : "false");
  51. }
  52. if (!this.IsCascadeCheck.HasValue || !this.IsCascadeCheck.Value)
  53. {
  54. dictOptions.Add("onBeforeCheck", "CMSFunction.Tree.OnBeforeCheck");
  55. }
  56. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  57. return dictPropertyList;
  58. }
  59. public static Tree CreateControl(HtmlHelper htmlHelper, TreeOptions treeOptions, IDictionary<string, string> attributes = null)
  60. {
  61. Tree tree = new Tree();
  62. if (attributes != null)
  63. {
  64. tree.Attributes = attributes;
  65. }
  66. treeOptions = treeOptions ?? new TreeOptions();
  67. //tree.CssClass = string.IsNullOrWhiteSpace(treeOptions.CssClass) ? "easyui-accordion" : treeOptions.CssClass;
  68. tree.CssClass = treeOptions.CssClass;
  69. tree.ID = treeOptions.ID;
  70. tree.Name = treeOptions.Name;
  71. //tree.IconCls = treeOptions.IconCls;
  72. //tree.DateItemCss = string.IsNullOrWhiteSpace(treeOptions.DateItemCss) ? "easyui-tree" : treeOptions.DateItemCss;
  73. tree.ItemSourceUrl = treeOptions.ItemSourceUrl;
  74. tree.IsCheckTree = treeOptions.IsCheckTree;
  75. tree.IsCascadeCheck = treeOptions.IsCascadeCheck;
  76. tree.UrlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  77. return tree;
  78. }
  79. }
  80. public class TreeItem
  81. {
  82. public string id { get; set; }
  83. public string text { get; set; }
  84. public string iconCls { get; set; }
  85. public bool @checked { get; set; }
  86. public object attributes { get; set; }
  87. public List<TreeItem> children { get; set; }
  88. }
  89. }