TableViewModel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections.Generic;
  2. using WeApp.Views.Shared.SearchForm;
  3. namespace WeApp.Views.Shared.Table
  4. {
  5. public class TableViewModel
  6. {
  7. public TableViewModel(string url, string activeMenu, SearchFormViewModel searchForm, string tableId = "table", bool hasBox = true) : this(tableId)
  8. {
  9. TableId = tableId;
  10. Url = url;
  11. ActiveMenu = activeMenu;
  12. SearchForm = searchForm;
  13. HasBox = hasBox;
  14. Other = "";
  15. }
  16. public TableViewModel(string url, string activeMenu = null, string tableId = "table", bool hasBox = true)
  17. : this(url, activeMenu, null, tableId, hasBox)
  18. {
  19. TableId = tableId;
  20. Url = url;
  21. ActiveMenu = activeMenu;
  22. SingleSelect = true;
  23. PageArray = new[] { 25, 50, 100, 200 };
  24. Other = "";
  25. }
  26. public TableViewModel(string url, string activeMenu, SearchFormViewModel searchForm, List<FieldItem> fieldItems,
  27. string tableId = "table")
  28. : this(url, activeMenu, searchForm, tableId)
  29. {
  30. TableId = tableId;
  31. Url = url;
  32. ActiveMenu = activeMenu;
  33. FieldItems = fieldItems;
  34. SearchForm = searchForm;
  35. }
  36. public TableViewModel(string tableId = "table")
  37. {
  38. TableId = tableId;
  39. PageArray = new[] { 25, 50, 100, 200 };
  40. SingleSelect = true;
  41. }
  42. public SearchFormViewModel SearchForm { get; set; }
  43. public string TableId { get; set; }
  44. public string Url { get; set; }
  45. public bool HasBox { get; set; }
  46. public string ActiveMenu { get; set; }
  47. public int[] PageArray { get; set; }
  48. public string PageArrayStr => string.Join(",", PageArray);
  49. public bool SingleSelect { get; set; }
  50. public string SingleSelectStr => SingleSelect ? "true" : "false";
  51. public bool IsLocalData { get; set; }
  52. public string LocalDataStr => IsLocalData ? "" : "data-method=\"post\" data-side-pagination=\"server\"";
  53. public string Other { get; set; }
  54. public string RowAttributes { get; set; }
  55. public List<FieldItem> FieldItems { get; set; }
  56. public TableViewModel SetPageArray(params int[] page)
  57. {
  58. PageArray = page;
  59. return this;
  60. }
  61. public TableViewModel SetMultipleSelect()
  62. {
  63. SingleSelect = false;
  64. return this;
  65. }
  66. public TableViewModel SetOther(string other)
  67. {
  68. Other = other ?? "";
  69. return this;
  70. }
  71. public TableViewModel SetFields(List<FieldItem> filedItems)
  72. {
  73. FieldItems = filedItems;
  74. return this;
  75. }
  76. public TableViewModel AddFields(FieldItem fieldItem)
  77. {
  78. FieldItems = FieldItems ?? new List<FieldItem>();
  79. FieldItems.Add(fieldItem);
  80. return this;
  81. }
  82. public TableViewModel SetLocalData()
  83. {
  84. IsLocalData = true;
  85. return this;
  86. }
  87. }
  88. public class FieldItem
  89. {
  90. public FieldItem(string filed, string displayName, string formatter = "", string align = "center", bool isTip = false, bool isSort = true)
  91. {
  92. Filed = filed;
  93. DisplayName = displayName;
  94. Formatter = formatter;
  95. Align = align;
  96. IsTip = isTip;
  97. IsSort = isSort;
  98. }
  99. public FieldItem SetWidth(int width, string unit = "px")
  100. {
  101. Width = width;
  102. WidthUnit = unit;
  103. return this;
  104. }
  105. public FieldItem SetTip()
  106. {
  107. IsTip = true;
  108. return this;
  109. }
  110. public FieldItem SetNotSort()
  111. {
  112. IsSort = false;
  113. return this;
  114. }
  115. public string Filed { get; set; }
  116. public string DisplayName { get; set; }
  117. public string Formatter { get; set; }
  118. public string Align { get; set; }
  119. public int? Width { get; set; }
  120. public string WidthUnit { get; set; }
  121. public string WidthStr => Width != null ? $" data-width=\"{Width}{WidthUnit}\"" : "";
  122. public bool IsSort { get; set; }
  123. public string IsSortStr => IsSort ? " data-sortable=\"true\"" : "";
  124. private bool IsTip { get; set; }
  125. public string IsTipStr => IsTip ? "data-class=\"iwb-tips\"" : "";
  126. }
  127. }