CheckList.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 CheckList : BaseControl
  9. {
  10. public CheckList()
  11. : base()
  12. {
  13. ItemList = new List<ListControlItem>();
  14. SelectedValues = new List<object>();
  15. }
  16. public int? Width { get; set; }
  17. public int? Height { get; set; }
  18. private int columnCount = 1;
  19. public int ColumnCount {
  20. get {
  21. return this.columnCount;
  22. }
  23. set
  24. {
  25. this.columnCount = value;
  26. }
  27. }
  28. public List<object> SelectedValues
  29. {
  30. get;
  31. set;
  32. }
  33. public List<ListControlItem> ItemList
  34. {
  35. get;
  36. private set;
  37. }
  38. public string ItemSourceUrl { get; set; }
  39. public string SelectedValueUrl { get; set; }
  40. public string TextField
  41. {
  42. get;
  43. set;
  44. }
  45. public string ValueField
  46. {
  47. get;
  48. set;
  49. }
  50. /// <summary>
  51. /// 是否显示
  52. /// </summary>
  53. public bool? IsShow { get; set; }
  54. /// <summary>
  55. /// 是否启用
  56. /// </summary>
  57. public bool? IsEnabled { get; set; }
  58. public string OnClick { get; set; }
  59. public string OnLoadSuccess { get; set; }
  60. protected IDictionary<string, string> GetPropertyList()
  61. {
  62. var dictPropertyList = new Dictionary<string, string>();
  63. if (string.IsNullOrEmpty(this.ID))
  64. {
  65. this.ID = "dynamic_checklist_" + DateTime.Now.ToString("yyyMMddHHmmssffff") + "_" + NewIDIndex();
  66. }
  67. dictPropertyList.Add("id", this.ID);
  68. if (!string.IsNullOrEmpty(this.Name))
  69. {
  70. dictPropertyList.Add("name", "div" + this.Name);
  71. }
  72. dictPropertyList.Add("class", string.Format("easyui-checkList {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  73. if (this.Width.HasValue)
  74. {
  75. dictPropertyList.Add("width", this.Width.Value.ToString() + "px");
  76. }
  77. if (this.Height.HasValue)
  78. {
  79. dictPropertyList.Add("height", this.Height.Value.ToString() + "px");
  80. }
  81. else
  82. {
  83. dictPropertyList.Add("height", "auto");
  84. }
  85. #region data-options
  86. var dictOptions = new Dictionary<string, string>();
  87. if (!string.IsNullOrEmpty(this.Name))
  88. {
  89. dictOptions.Add("checkName", "'" + this.Name + "'");
  90. }
  91. else
  92. {
  93. dictOptions.Add("checkName", "'" + this.ID + "'");
  94. }
  95. dictOptions.Add("columnCount", this.ColumnCount.ToString());
  96. if (!string.IsNullOrEmpty(this.ItemSourceUrl))
  97. {
  98. dictOptions.Add("url", "'" + this.ItemSourceUrl + "'");
  99. if (string.IsNullOrEmpty(this.TextField))
  100. {
  101. throw new Exception("指定了ItemSourceUrl就一定要设置TextField");
  102. }
  103. if (string.IsNullOrEmpty(this.ValueField))
  104. {
  105. throw new Exception("指定了ItemSourceUrl就一定要设置ValueField");
  106. }
  107. }
  108. if (this.IsShow.HasValue && this.IsShow == false)
  109. {
  110. dictOptions.Add("isShow", "false");
  111. }
  112. else
  113. {
  114. dictOptions.Add("isShow", "true");
  115. }
  116. if (this.IsEnabled.HasValue && this.IsEnabled == false)
  117. {
  118. dictOptions.Add("isEnabled", "false");
  119. }
  120. else
  121. {
  122. dictOptions.Add("isEnabled", "true");
  123. }
  124. if (!string.IsNullOrEmpty(this.TextField))
  125. {
  126. dictOptions.Add("textField", "'" + this.TextField + "'");
  127. }
  128. if (!string.IsNullOrEmpty(this.ValueField))
  129. {
  130. dictOptions.Add("valueField", "'" + this.ValueField + "'");
  131. }
  132. if (this.SelectedValueUrl != null)
  133. {
  134. dictOptions.Add("valueUrl", "'" + this.SelectedValueUrl + "'");
  135. }
  136. if (!string.IsNullOrEmpty(this.OnClick))
  137. {
  138. dictOptions.Add("onclick", this.OnClick);
  139. }
  140. if (!string.IsNullOrEmpty(this.OnLoadSuccess))
  141. {
  142. dictOptions.Add("onLoadSuccess", this.OnLoadSuccess);
  143. }
  144. if (ItemList.Count > 0)
  145. {
  146. dictOptions.Add("data",
  147. string.Concat("[",
  148. string.Join(",", ItemList.Select(x => string.Format("{{ {0}:'{1}',{2}:'{3}' }}",
  149. (string.IsNullOrEmpty(this.TextField) ? "Text" : this.TextField), x.Text,
  150. (string.IsNullOrEmpty(this.ValueField) ? "Value" : this.ValueField), x.Value))),
  151. "]"));
  152. }
  153. if (SelectedValues.Count > 0)
  154. {
  155. dictOptions.Add("values",
  156. string.Concat("[",
  157. string.Join(",", SelectedValues.Select(x => "'" + x.ToString() + "'")),
  158. "]"));
  159. }
  160. //dictOptions.Add("required", "true");
  161. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  162. #endregion data-options
  163. return dictPropertyList;
  164. }
  165. public override string Render()
  166. {
  167. TagBuilder tabBuilder = new TagBuilder("div");
  168. tabBuilder.MergeAttributes<string, string>(GetPropertyList());
  169. if (this.Attributes != null)
  170. {
  171. tabBuilder.MergeAttributes<string, string>(this.Attributes);
  172. }
  173. //StringBuilder checkListHtml = new StringBuilder();
  174. //if (ItemList.Count > 0)
  175. //{
  176. // int pages = Convert.ToInt32(
  177. // Math.Ceiling((Double)ItemList.Count / this.columnCount)
  178. // );
  179. // checkListHtml.Append("<table border='0' width='100%'>");
  180. // for (int i = 0; i < pages; i ++)
  181. // {
  182. // checkListHtml.Append("<tr>");
  183. // for (int j = 0; j < this.columnCount; j ++)
  184. // {
  185. // int curIndex = (i * this.columnCount + j);
  186. // if (ItemList.Count > curIndex)
  187. // {
  188. // checkListHtml.Append("<td>");
  189. // checkListHtml.AppendFormat("<input name=\"{0}\" {3} type=checkbox value=\"{1}\"/>{2}",
  190. // this.Name,
  191. // ItemList[curIndex].Value,
  192. // ItemList[curIndex].Text,
  193. // ((this.SelectedValues.Contains(ItemList[curIndex].Value)) ? "checked" : "")
  194. // );
  195. // checkListHtml.Append("</td>");
  196. // }
  197. // else
  198. // {
  199. // checkListHtml.Append("<td>&nbsp;</td>");
  200. // }
  201. // }
  202. // checkListHtml.Append("</tr>");
  203. // }
  204. // checkListHtml.Append("</table>");
  205. // tabBuilder.InnerHtml = checkListHtml.ToString();
  206. //}
  207. return tabBuilder.ToString(TagRenderMode.Normal);
  208. }
  209. public static CheckList CreateControl(ListControlOptions listControlOptions, IDictionary<string, string> attributes = null)
  210. {
  211. CheckList checkList = new CheckList();
  212. checkList.CssClass = listControlOptions.CssClass;
  213. checkList.ID = listControlOptions.ID;
  214. checkList.ItemList.AddRange(listControlOptions.ItemList);
  215. checkList.Name = listControlOptions.Name;
  216. checkList.IsShow = listControlOptions.IsShow;
  217. checkList.IsEnabled = listControlOptions.IsEnabled;
  218. checkList.SelectedValues = listControlOptions.SelectedValues;
  219. checkList.ItemSourceUrl = listControlOptions.ItemSourceUrl;
  220. checkList.SelectedValueUrl = listControlOptions.SelectedValueUrl;
  221. checkList.TextField = string.IsNullOrWhiteSpace(listControlOptions.TextField) ? "Text" : listControlOptions.TextField;
  222. checkList.ValueField = string.IsNullOrWhiteSpace(listControlOptions.ValueField) ? "Value" : listControlOptions.ValueField;
  223. checkList.Width = listControlOptions.Width;
  224. checkList.ColumnCount = listControlOptions.ColumnCount;
  225. checkList.OnClick = listControlOptions.OnClick;
  226. checkList.OnLoadSuccess = listControlOptions.OnLoadSuccess;
  227. return checkList;
  228. }
  229. }
  230. }