TableViewModel.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System.Collections.Generic;
  2. using System.Web.WebPages;
  3. using ContractService.Views.Shared.SearchForm;
  4. namespace ContractService.Views.Shared.Table
  5. {
  6. public class TableViewModel
  7. {
  8. public TableViewModel()
  9. {
  10. IsPage = true;
  11. IsClickSelect = true;
  12. IsSingleSelect = true;
  13. }
  14. public TableViewModel(string tableId = "table") : this()
  15. {
  16. TableId = tableId;
  17. PageArray = new[] { 25, 50, 100, 200 };
  18. IsSingleSelect = true;
  19. }
  20. public TableViewModel(string url, string currentPage, SearchFormViewModel searchForm, string tableId = "table", bool hasBox = true) : this(tableId)
  21. {
  22. TableId = tableId;
  23. Url = url;
  24. CurrentPage = currentPage;
  25. SearchForm = searchForm;
  26. HasBox = hasBox;
  27. Other = "";
  28. }
  29. public TableViewModel(string url, string currentPage = null, string tableId = "table", bool hasBox = true)
  30. : this(url, currentPage, null, tableId, hasBox)
  31. {
  32. TableId = tableId;
  33. Url = url;
  34. CurrentPage = currentPage;
  35. IsSingleSelect = true;
  36. PageArray = new[] { 25, 50, 100, 200 };
  37. Other = "";
  38. }
  39. public TableViewModel(string url, string currentPage, SearchFormViewModel searchForm, List<FieldItem> fieldItems,
  40. string tableId = "table")
  41. : this(url, currentPage, searchForm, tableId)
  42. {
  43. TableId = tableId;
  44. Url = url;
  45. CurrentPage = currentPage;
  46. FieldItems = fieldItems;
  47. SearchForm = searchForm;
  48. }
  49. public SearchFormViewModel SearchForm { get; set; }
  50. public string TableId { get; set; }
  51. public string _fieldId;
  52. public string FieldId => _fieldId.IsEmpty() ? "id" : _fieldId;
  53. public string Url { get; set; }
  54. public bool HasBox { get; set; }
  55. public string CurrentPage { get; set; }
  56. public bool IsPage { get; set; }
  57. public string PageStr => IsPage ? "true" : "false";
  58. public int[] PageArray { get; set; }
  59. public string PageArrayStr => string.Join(",", PageArray);
  60. public bool IsSingleSelect { get; set; }
  61. public string SingleSelectStr => IsSingleSelect ? "true" : "false";
  62. public bool IsClickSelect { get; set; }
  63. public string ClickSelectStr => IsClickSelect ? "true" : "false";
  64. public bool IsLocalData { get; set; }
  65. public string LocalDataStr => IsLocalData ? "" : "data-method=\"post\" data-side-pagination=\"server\"";
  66. public string Other { get; set; }
  67. public string RowAttributes { get; set; }
  68. public List<FieldItem> FieldItems { get; set; }
  69. public TableViewModel SetTable(string active, string url = "", bool hasBox = true, string id = "table")
  70. {
  71. CurrentPage = active;
  72. TableId = id;
  73. Url = url;
  74. HasBox = HasBox;
  75. return this;
  76. }
  77. public TableViewModel SetId(string id)
  78. {
  79. TableId = id;
  80. return this;
  81. }
  82. public TableViewModel SetFieldId(string fieldId)
  83. {
  84. _fieldId = fieldId;
  85. return this;
  86. }
  87. public TableViewModel SetNotBox()
  88. {
  89. HasBox = false;
  90. return this;
  91. }
  92. public TableViewModel SetSearchForm(SearchFormViewModel search)
  93. {
  94. SearchForm = search;
  95. return this;
  96. }
  97. public TableViewModel SetNotPage()
  98. {
  99. IsPage = false;
  100. return this;
  101. }
  102. public TableViewModel SetPageArray(params int[] page)
  103. {
  104. IsPage = true;
  105. PageArray = page;
  106. return this;
  107. }
  108. public TableViewModel SetMultipleSelect()
  109. {
  110. IsSingleSelect = false;
  111. return this;
  112. }
  113. public TableViewModel SetRowAttributes(string rowAttributes)
  114. {
  115. RowAttributes = rowAttributes ?? "";
  116. return this;
  117. }
  118. public TableViewModel SetOther(string other)
  119. {
  120. Other = other ?? "";
  121. return this;
  122. }
  123. public TableViewModel SetFields(List<FieldItem> filedItems)
  124. {
  125. FieldItems = filedItems;
  126. return this;
  127. }
  128. public TableViewModel AddFields(List<FieldItem> filedItems)
  129. {
  130. FieldItems = FieldItems ?? new List<FieldItem>();
  131. FieldItems.AddRange(filedItems);
  132. return this;
  133. }
  134. public TableViewModel AddField(FieldItem fieldItem)
  135. {
  136. FieldItems = FieldItems ?? new List<FieldItem>();
  137. FieldItems.Add(fieldItem);
  138. return this;
  139. }
  140. public TableViewModel SetLocalData()
  141. {
  142. IsLocalData = true;
  143. return this;
  144. }
  145. public TableViewModel SetClickSelect(bool isClickSelect = true)
  146. {
  147. IsClickSelect = isClickSelect;
  148. return this;
  149. }
  150. public TableViewModel SetSingleSelect(bool isSingleSelect = true)
  151. {
  152. IsSingleSelect = isSingleSelect;
  153. return this;
  154. }
  155. }
  156. public class FieldItem
  157. {
  158. public FieldItem(string filed, string displayName, string formatter = "", string align = "center", bool isTip = false, bool isSort = false)
  159. {
  160. Filed = filed;
  161. DisplayName = displayName;
  162. Formatter = formatter;
  163. Align = align;
  164. IsTip = isTip;
  165. IsSort = isSort;
  166. }
  167. public FieldItem SetWidth(int width, string unit = "px")
  168. {
  169. Width = width;
  170. WidthUnit = unit;
  171. return this;
  172. }
  173. public FieldItem SetTip()
  174. {
  175. IsTip = true;
  176. return this;
  177. }
  178. public FieldItem SetSort()
  179. {
  180. IsSort = true;
  181. return this;
  182. }
  183. public FieldItem SetRight()
  184. {
  185. Align = "right";
  186. return this;
  187. }
  188. public FieldItem SetLeft()
  189. {
  190. Align = "left";
  191. return this;
  192. }
  193. public string Filed { get; set; }
  194. public string DisplayName { get; set; }
  195. public string Formatter { get; set; }
  196. public string FormatterStr => string.IsNullOrEmpty(Formatter) ? "" : "data-formatter=\"" + Formatter + "\"";
  197. public string Align { get; set; }
  198. public int? Width { get; set; }
  199. public string WidthUnit { get; set; }
  200. public string WidthStr => Width != null ? $" data-width=\"{Width}{WidthUnit}\"" : "";
  201. public bool IsSort { get; set; }
  202. public string IsSortStr => IsSort ? " data-sortable=\"true\"" : "";
  203. private bool IsTip { get; set; }
  204. public string IsTipStr => IsTip ? "data-class=\"iwb-tips\"" : "";
  205. }
  206. }