QQList.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 QQList : BaseControl
  9. {
  10. public QQList()
  11. : base()
  12. {
  13. ItemList = new List<ListControlItem>();
  14. }
  15. public int? Width { get; set; }
  16. public int? ItemWidth { get; set; }
  17. public int? ItemHeight { 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<ListControlItem> ItemList
  29. {
  30. get;
  31. private set;
  32. }
  33. public string ItemSourceUrl { get; set; }
  34. public string TextField
  35. {
  36. get;
  37. set;
  38. }
  39. public string ValueField
  40. {
  41. get;
  42. set;
  43. }
  44. /// <summary>
  45. /// 是否启用
  46. /// </summary>
  47. public bool? IsEnabled { get; set; }
  48. public string OnClick { get; set; }
  49. public string OnLoadSuccess { get; set; }
  50. protected IDictionary<string, string> GetPropertyList()
  51. {
  52. var dictPropertyList = new Dictionary<string, string>();
  53. if (string.IsNullOrEmpty(this.ID))
  54. {
  55. this.ID = "dynamic_qqlist_" + DateTime.Now.ToString("yyyMMddHHmmssffff") + "_" + NewIDIndex();
  56. }
  57. dictPropertyList.Add("id", this.ID);
  58. if (!string.IsNullOrEmpty(this.Name))
  59. {
  60. dictPropertyList.Add("name", "div" + this.Name);
  61. }
  62. dictPropertyList.Add("class", string.Format("easyui-qqList {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  63. if (this.Width.HasValue)
  64. {
  65. if (this.Width.Value > 1)
  66. {
  67. dictPropertyList.Add("width", this.Width.Value.ToString() + "px");
  68. }
  69. else
  70. {
  71. dictPropertyList.Add("width", (this.Width.Value * 100).ToString() + "%");
  72. }
  73. }
  74. #region data-options
  75. var dictOptions = new Dictionary<string, string>();
  76. if (!string.IsNullOrEmpty(this.Name))
  77. {
  78. dictOptions.Add("itemName", "'" + this.Name + "'");
  79. }
  80. else
  81. {
  82. dictOptions.Add("itemName", "'" + this.ID + "'");
  83. }
  84. dictOptions.Add("columnCount", this.ColumnCount.ToString());
  85. if (this.ItemWidth.HasValue)
  86. {
  87. dictOptions.Add("itemWidth", this.ItemWidth.Value.ToString() + "px");
  88. }
  89. else
  90. {
  91. dictOptions.Add("itemWidth", "32px");
  92. }
  93. if (this.ItemHeight.HasValue)
  94. {
  95. dictOptions.Add("itemHeight", this.ItemHeight.Value.ToString() + "px");
  96. }
  97. else
  98. {
  99. dictOptions.Add("itemHeight", "32px");
  100. }
  101. if (!string.IsNullOrEmpty(this.ItemSourceUrl))
  102. {
  103. dictOptions.Add("url", "'" + this.ItemSourceUrl + "'");
  104. if (string.IsNullOrEmpty(this.TextField))
  105. {
  106. throw new Exception("指定了ItemSourceUrl就一定要设置TextField");
  107. }
  108. if (string.IsNullOrEmpty(this.ValueField))
  109. {
  110. throw new Exception("指定了ItemSourceUrl就一定要设置ValueField");
  111. }
  112. }
  113. if (this.IsEnabled.HasValue && this.IsEnabled == false)
  114. {
  115. dictOptions.Add("isEnabled", "false");
  116. }
  117. else
  118. {
  119. dictOptions.Add("isEnabled", "true");
  120. }
  121. if (!string.IsNullOrEmpty(this.TextField))
  122. {
  123. dictOptions.Add("textField", "'" + this.TextField + "'");
  124. }
  125. if (!string.IsNullOrEmpty(this.ValueField))
  126. {
  127. dictOptions.Add("valueField", "'" + this.ValueField + "'");
  128. }
  129. if (!string.IsNullOrEmpty(this.OnClick))
  130. {
  131. dictOptions.Add("onclick", this.OnClick);
  132. }
  133. if (!string.IsNullOrEmpty(this.OnLoadSuccess))
  134. {
  135. dictOptions.Add("onLoadSuccess", this.OnLoadSuccess);
  136. }
  137. if (ItemList.Count > 0)
  138. {
  139. dictOptions.Add("data",
  140. string.Concat("[",
  141. string.Join(",", ItemList.Select(x => string.Format("{{ {0}:'{1}',{2}:'{3}' }}",
  142. (string.IsNullOrEmpty(this.TextField) ? "Text" : this.TextField), x.Text,
  143. (string.IsNullOrEmpty(this.ValueField) ? "Value" : this.ValueField), x.Value))),
  144. "]"));
  145. }
  146. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  147. #endregion data-options
  148. return dictPropertyList;
  149. }
  150. public override string Render()
  151. {
  152. TagBuilder tabBuilder = new TagBuilder("div");
  153. tabBuilder.MergeAttributes<string, string>(GetPropertyList());
  154. if (this.Attributes != null)
  155. {
  156. tabBuilder.MergeAttributes<string, string>(this.Attributes);
  157. }
  158. return tabBuilder.ToString(TagRenderMode.Normal);
  159. }
  160. public static QQList CreateControl(QQListOptions qqListOptions, IDictionary<string, string> attributes = null)
  161. {
  162. QQList qqList = new QQList();
  163. qqList.CssClass = qqListOptions.CssClass;
  164. qqList.ID = qqListOptions.ID;
  165. qqList.ItemList.AddRange(qqListOptions.ItemList);
  166. qqList.Name = qqListOptions.Name;
  167. qqList.IsShow = qqListOptions.IsShow;
  168. qqList.IsEnabled = qqListOptions.IsEnabled;
  169. qqList.ItemSourceUrl = qqListOptions.ItemSourceUrl;
  170. qqList.TextField = string.IsNullOrWhiteSpace(qqListOptions.TextField) ? "Text" : qqListOptions.TextField;
  171. qqList.ValueField = string.IsNullOrWhiteSpace(qqListOptions.ValueField) ? "Value" : qqListOptions.ValueField;
  172. qqList.Width = qqListOptions.Width;
  173. qqList.ItemWidth = qqListOptions.ItemWidth;
  174. qqList.ItemHeight = qqListOptions.ItemHeight;
  175. qqList.ColumnCount = qqListOptions.ColumnCount;
  176. qqList.OnClick = qqListOptions.OnClick;
  177. qqList.OnLoadSuccess = qqListOptions.OnLoadSuccess;
  178. return qqList;
  179. }
  180. }
  181. }