Combotree.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Bowin.Web.Controls.Mvc
  6. {
  7. public class Combotree : BaseControl
  8. {
  9. public Combotree()
  10. : base()
  11. {
  12. Enable = true;
  13. }
  14. public bool? Enable { get; set; }
  15. public bool Required { get; set; }
  16. public string DataSourceUrl { get; set; }
  17. public string Style { get; set; }
  18. public object SelectedValue { get; set; }
  19. public string DataCondition { get; set; }
  20. public override string Render()
  21. {
  22. StringBuilder htmlBuilder = new StringBuilder();
  23. htmlBuilder.Append("<input ");
  24. htmlBuilder.Append(string.Join(" ", GetPropertyList().Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value))));
  25. htmlBuilder.AppendLine(" />");
  26. return htmlBuilder.ToString();
  27. }
  28. public static BaseControl CreateControl(CombotreeOptions combotreeOptions, IDictionary<string, object> attributes = null)
  29. {
  30. Combotree combotree = new Combotree();
  31. combotree.ID = combotreeOptions.ID;
  32. combotree.CssClass = combotreeOptions.CssClass;
  33. combotree.DataSourceUrl = combotreeOptions.DataSourceUrl;
  34. combotree.Style = combotreeOptions.Style;
  35. //combotree.Required = combotreeOptions.Required;
  36. combotree.SelectedValue = combotreeOptions.SelectedValue;
  37. combotree.Name = combotreeOptions.Name;
  38. combotree.DataCondition = combotreeOptions.DataCondition;
  39. if (attributes != null)
  40. {
  41. Dictionary<string, string> _attr = new Dictionary<string, string>();
  42. attributes.ToList().ForEach(it =>
  43. {
  44. _attr.Add(it.Key, (it.Value ?? "").ToString());
  45. });
  46. combotree.Attributes = _attr;
  47. }
  48. return combotree;
  49. }
  50. protected Dictionary<string, string> GetPropertyList()
  51. {
  52. var dictPropertyList = new Dictionary<string, string>();
  53. if (!string.IsNullOrEmpty(this.ID))
  54. {
  55. dictPropertyList.Add("id", this.ID);
  56. }
  57. if (!string.IsNullOrEmpty(this.Name))
  58. {
  59. dictPropertyList.Add("name", this.Name);
  60. }
  61. if (!string.IsNullOrEmpty(this.DataCondition))
  62. {
  63. dictPropertyList.Add("data-condition", this.Name);
  64. }
  65. //if (this.SelectedValue != null)
  66. //{
  67. // dictPropertyList.Add("value", this.SelectedValue.ToString());
  68. //}
  69. if (!string.IsNullOrEmpty(Style))
  70. dictPropertyList.Add("style", this.Style);
  71. dictPropertyList.Add("class", string.Format("easyui-combotree {0}", this.CssClass == null ? "" : this.CssClass));
  72. #region data-options
  73. var dictOptions = new Dictionary<string, string>();
  74. if (!string.IsNullOrWhiteSpace(this.DataSourceUrl))
  75. {
  76. dictOptions.Add("url", "'" + this.DataSourceUrl + "'");
  77. }
  78. if (this.SelectedValue != null)
  79. {
  80. dictOptions.Add("value", "'" + this.SelectedValue.ToString() + "'");
  81. }
  82. //dictOptions.Add("required", "'" + this.Required.ToString() + "'");
  83. if (Attributes.ContainsKey("data-val-required"))
  84. {
  85. //dictOptions.Add("required", "'false'");
  86. //dictOptions.Add("missingMessage", "'" + Attributes["data-val-required"] + "'");
  87. }
  88. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  89. #endregion
  90. //Where(w => w.Key != "data-val")
  91. Attributes.ToList().ForEach(it =>
  92. {
  93. dictPropertyList.Add(it.Key, it.Value);
  94. });
  95. return dictPropertyList;
  96. }
  97. }
  98. }