ComboGrid.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 ComboGrid : BaseControl
  9. {
  10. public ComboGridOptions ComboGridOptions { get; set; }
  11. public IDictionary<string, string> GridAttributes { get; set; }
  12. public static ComboGrid CreateControl(ComboGridOptions comboGridOptions, IDictionary<string, string> attributes = null, IDictionary<string, string> gridAttributes = null)
  13. {
  14. ComboGrid comboGrid = new ComboGrid();
  15. comboGrid.ComboGridOptions = comboGridOptions;
  16. comboGrid.ComboGridOptions.GridOptions.ID = comboGrid.ComboGridOptions.ID + "_gridX";
  17. comboGrid.ComboGridOptions.GridOptions.Name = comboGrid.ComboGridOptions.Name + "_gridX";
  18. comboGrid.ComboGridOptions.GridOptions.PagerType = PagerType.Simple;
  19. if (!comboGrid.ComboGridOptions.GridOptions.PageSize.HasValue)
  20. {
  21. comboGrid.ComboGridOptions.GridOptions.PageSize = 10;
  22. }
  23. comboGrid.Attributes = attributes;
  24. comboGrid.GridAttributes = gridAttributes;
  25. return comboGrid;
  26. }
  27. protected IDictionary<string, string> GetPropertyList()
  28. {
  29. var dictPropertyList = new Dictionary<string, string>();
  30. dictPropertyList.Add("id", this.ComboGridOptions.ID);
  31. if (!string.IsNullOrEmpty(this.ComboGridOptions.Name))
  32. {
  33. dictPropertyList.Add("comboname", this.ComboGridOptions.Name);
  34. }
  35. dictPropertyList.Add("class", string.Format("easyui-combogridX {0}", string.IsNullOrEmpty(this.ComboGridOptions.CssClass) ? "" : this.ComboGridOptions.CssClass));
  36. if (this.ComboGridOptions.Width.HasValue)
  37. {
  38. dictPropertyList.Add("width", this.ComboGridOptions.Width.ToString());
  39. }
  40. if (this.ComboGridOptions.IsShow.HasValue && !this.ComboGridOptions.IsShow.Value)
  41. {
  42. if (dictPropertyList.Keys.Contains("style"))
  43. dictPropertyList["style"] += "display:none;";
  44. else
  45. dictPropertyList.Add("style", "display:none;");
  46. }
  47. #region data-options
  48. var dictOptions = new Dictionary<string, string>();
  49. if (string.IsNullOrEmpty(this.ComboGridOptions.TextField))
  50. {
  51. throw new Exception("必须指定TextField");
  52. }
  53. if (string.IsNullOrEmpty(this.ComboGridOptions.ValueField))
  54. {
  55. throw new Exception("必须指定ValueField");
  56. }
  57. dictOptions.Add("textField", "'" + this.ComboGridOptions.TextField + "'");
  58. dictOptions.Add("valueField", "'" + this.ComboGridOptions.ValueField + "'");
  59. if (this.ComboGridOptions.IsAutoComplete.HasValue)
  60. {
  61. dictOptions.Add("isAutoComplete", this.ComboGridOptions.IsAutoComplete.Value ? "true" : "false");
  62. if (this.ComboGridOptions.MinReloadCharactor.HasValue)
  63. {
  64. dictOptions.Add("minReloadCharactor", this.ComboGridOptions.MinReloadCharactor.Value.ToString());
  65. }
  66. }
  67. if (!string.IsNullOrEmpty(this.ComboGridOptions.OnSelect))
  68. {
  69. dictOptions.Add("onSelect", this.ComboGridOptions.OnSelect);
  70. }
  71. if (!string.IsNullOrEmpty(this.ComboGridOptions.OnChange))
  72. {
  73. dictOptions.Add("onChange", this.ComboGridOptions.OnChange);
  74. }
  75. if (this.ComboGridOptions.Width.HasValue && !this.ComboGridOptions.PanelWidth.HasValue)
  76. {
  77. dictOptions.Add("panelWidth", Math.Ceiling((double)this.ComboGridOptions.Width.Value * 1.5).ToString());
  78. }
  79. else if (this.ComboGridOptions.PanelWidth.HasValue)
  80. {
  81. dictOptions.Add("panelWidth", this.ComboGridOptions.PanelWidth.ToString());
  82. }
  83. if (this.ComboGridOptions.Height.HasValue)
  84. {
  85. dictOptions.Add("panelHeight", Math.Ceiling((double)this.ComboGridOptions.Height.Value * 1.5).ToString());
  86. }
  87. if (ComboGridOptions.SelectedValue != null &&
  88. ComboGridOptions.SelectedValue.ToString() != "")
  89. {
  90. dictOptions.Add("value", "'" + this.ComboGridOptions.SelectedValue + "'");
  91. }
  92. else
  93. {
  94. dictOptions.Add("value", "'-1'");
  95. }
  96. if (!string.IsNullOrEmpty(ComboGridOptions.SelectedText))
  97. {
  98. dictOptions.Add("text", "'" + this.ComboGridOptions.SelectedText + "'");
  99. }
  100. else
  101. {
  102. dictOptions.Add("text", "''");
  103. }
  104. if (ComboGridOptions.SelectedIndex.HasValue)
  105. {
  106. dictOptions.Add("selectedIndex", this.ComboGridOptions.SelectedIndex.Value.ToString());
  107. }
  108. if (!string.IsNullOrEmpty(ComboGridOptions.EmptyText))
  109. {
  110. dictOptions.Add("emptyText", "'" + this.ComboGridOptions.EmptyText + "'");
  111. }
  112. if (ComboGridOptions.IsEnabled == false)
  113. {
  114. dictOptions.Add("enabled", "false");
  115. }
  116. else
  117. {
  118. dictOptions.Add("enabled", "true");
  119. }
  120. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  121. #endregion data-options
  122. return dictPropertyList;
  123. }
  124. public override string Render()
  125. {
  126. StringBuilder html = new StringBuilder();
  127. TagBuilder tabBuilder = new TagBuilder("span");
  128. tabBuilder.MergeAttributes<string, string>(GetPropertyList());
  129. if (this.Attributes != null)
  130. {
  131. tabBuilder.MergeAttributes<string, string>(this.Attributes);
  132. }
  133. TagBuilder tagInput = new TagBuilder("input");
  134. tagInput.AddCssClass("combo-text");
  135. if (this.ComboGridOptions.IsRequired == true)
  136. {
  137. tagInput.AddCssClass("validatebox-text");
  138. }
  139. tagInput.Attributes.Add("type", "text");
  140. if (!string.IsNullOrEmpty(this.ComboGridOptions.EmptyText))
  141. {
  142. //tagInput.Attributes.Add("placeholder", this.ComboGridOptions.EmptyText);
  143. tagInput.Attributes.Add("value", this.ComboGridOptions.EmptyText);
  144. tagInput.Attributes.Add("onfocus", "if (value =='" + this.ComboGridOptions.EmptyText + "'){value =''}");
  145. tagInput.Attributes.Add("onblur", "if (value ==''){value ='" + this.ComboGridOptions.EmptyText + "'}");
  146. }
  147. if (ComboGridOptions.IsEnabled == false)
  148. {
  149. tagInput.Attributes.Add("disabled", "disabled");
  150. }
  151. if ((this.ComboGridOptions.IsAutoComplete ?? true) == false)
  152. {
  153. tagInput.Attributes.Add("readonly", "true");
  154. }
  155. TagBuilder tagHidden = new TagBuilder("input");
  156. if (!string.IsNullOrEmpty(this.ComboGridOptions.Name))
  157. {
  158. tagHidden.Attributes.Add("name", this.ComboGridOptions.Name);
  159. }
  160. tagHidden.Attributes.Add("type", "hidden");
  161. tagHidden.AddCssClass("combogridX-value");
  162. html.Append(tabBuilder.ToString(TagRenderMode.StartTag));
  163. html.Append(tagInput.ToString(TagRenderMode.SelfClosing));
  164. html.Append("<span><span class=\"combo-clear\"></span><span class=\"combo-arrow combogridX-arrow\"></span></span>");
  165. html.Append(tagHidden.ToString(TagRenderMode.SelfClosing));
  166. html.Append("<div class=\"easyui-panel combogridX-expander\">");
  167. if (this.ComboGridOptions.GridOptions.Columns.Count <= 1)
  168. {
  169. this.ComboGridOptions.GridOptions.IsShowHeader = false;
  170. }
  171. html.Append(DataGrid.CreateControl(this.ComboGridOptions.GridOptions, this.GridAttributes).Render());
  172. html.Append("</div>");
  173. html.Append(tabBuilder.ToString(TagRenderMode.EndTag));
  174. return html.ToString();
  175. }
  176. }
  177. }