VmQueryModal.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using VberAdmin.Web.Models.Input;
  2. using VberAdmin.Web.Models.Search;
  3. using VberZero.Tools.StringModel;
  4. namespace VberAdmin.Web.Models.Modals;
  5. using VberAdmin.Web.Models.Table;
  6. public class VmQueryModal
  7. {
  8. /// <summary>
  9. ///
  10. /// </summary>
  11. /// <param name="name"></param>
  12. /// <param name="url"></param>
  13. /// <param name="modalId"></param>
  14. /// <param name="queryItems"></param>
  15. /// <param name="originField">"id,name,name2,displayName"</param>
  16. /// <param name="targetField">"id,name" "1:name,3:displayName" "1:name|name2,3:displayName"</param>
  17. /// <param name="ajaxSelectNameField"></param>
  18. /// <param name="submitEventName"></param>
  19. /// <param name="itemDbClickEventName"></param>
  20. /// <param name="itemClickEventName"></param>
  21. /// <param name="isSingleSelect"></param>
  22. /// <param name="vmQueryTreeSearch"></param>
  23. /// <param name="searchBindFun"></param>
  24. /// <param name="modalWidth"></param>
  25. public VmQueryModal(string name, string url, string modalId, List<VmQueryItem> queryItems, string originField,
  26. string targetField,
  27. string ajaxSelectNameField = "", string submitEventName = null, string itemDbClickEventName = null,
  28. string itemClickEventName = null, bool isSingleSelect = true, VmQueryTreeSearch vmQueryTreeSearch = null,
  29. string searchBindFun = "", int modalWidth = 900)
  30. {
  31. ModalName = name;
  32. QueryUrl = url;
  33. QueryItems = queryItems;
  34. SearchBindFun = searchBindFun;
  35. AjaxSelectNameField = ajaxSelectNameField;
  36. VmQueryTreeSearch = vmQueryTreeSearch;
  37. OriginFields = originField.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  38. var targets = new string[OriginFields.Length];
  39. TargetFields = new List<string[]>();
  40. IsSingleSelect = isSingleSelect;
  41. if (string.IsNullOrEmpty(targetField))
  42. {
  43. for (var i = 0; i < targets.Length; i++) TargetFields.Add(new[] { $"[name='{OriginFields[i]}']" });
  44. }
  45. else
  46. {
  47. var targetArr = targetField.Split(new[] { ',' }, StringSplitOptions.None);
  48. for (var i = 0; i < targets.Length; i++)
  49. if (i < targetArr.Length)
  50. {
  51. if (!string.IsNullOrEmpty(targetArr[i]))
  52. {
  53. var unitArr = targetArr[i].Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  54. if (unitArr.Length > 1)
  55. {
  56. if (int.TryParse(unitArr[0], out var index) && index < OriginFields.Length && index >= i)
  57. {
  58. for (; i < index; i++) TargetFields.Add(new[] { $"[name='{OriginFields[i]}']" });
  59. AddTargetField(unitArr[1], i);
  60. }
  61. else
  62. {
  63. AddTargetField(unitArr[1], i);
  64. }
  65. }
  66. else
  67. {
  68. AddTargetField(targetArr[i], i);
  69. }
  70. }
  71. else
  72. {
  73. TargetFields.Add(new[] { $"[name='{OriginFields[i]}']" });
  74. }
  75. }
  76. else
  77. {
  78. TargetFields.Add(new[] { $"[name='{OriginFields[i]}']" });
  79. }
  80. }
  81. SubmitEventName = submitEventName;
  82. ModalId = modalId;
  83. ModalWidth = modalWidth;
  84. ItemClickEventName = itemClickEventName;
  85. ItemDbClickEventName = itemDbClickEventName ?? submitEventName;
  86. }
  87. public string QueryUrl { get; set; }
  88. public List<VmQueryItem> QueryItems { get; set; }
  89. public string ModalId { get; set; }
  90. public string ModalName { get; set; }
  91. public int ModalWidth { get; set; }
  92. public string RowId { get; set; }
  93. public string[] OriginFields { get; set; }
  94. public List<string[]> TargetFields { get; set; }
  95. public string SubmitEventName { get; set; }
  96. public string ItemClickEventName { get; set; }
  97. public string ItemDbClickEventName { get; set; }
  98. public VmQueryTreeSearch VmQueryTreeSearch { get; set; }
  99. public string SearchBindFun { get; set; }
  100. public bool IsSingleSelect { get; set; }
  101. public bool IsMultiplePageSelect { private get; set; }
  102. public string PageSelect => IsMultiplePageSelect ? "true" : "false";
  103. public string SingleSelect => IsSingleSelect ? "true" : "false";
  104. public string AjaxSelectNameField { get; set; }
  105. private void AddTargetField(string targetStr, int index)
  106. {
  107. var target = targetStr.Split(new[] { '|' }, StringSplitOptions.None);
  108. for (var i = 0; i < target.Length; i++)
  109. target[i] = string.IsNullOrEmpty(target[i])
  110. ? $"[name='{OriginFields[index]}']"
  111. : target[i].StartsWith("#") || target[i].StartsWith(".")
  112. ? target[i]
  113. : $"[name='{target[i]}']";
  114. TargetFields.Add(target);
  115. }
  116. public VmQueryModal WithSearchFun(string funName)
  117. {
  118. SearchBindFun = funName;
  119. return this;
  120. }
  121. public VmQueryModal WithSearchTree(VmQueryTreeSearch searchTree)
  122. {
  123. VmQueryTreeSearch = searchTree;
  124. return this;
  125. }
  126. public VmSearch Search
  127. {
  128. get
  129. {
  130. var list = QueryItems.Where(a => a.IsSearch).ToList();
  131. if (list.Any())
  132. {
  133. var search = new List<VmSearchItem>();
  134. foreach (var item in list)
  135. {
  136. var s = new VmSearchItem(item.FiledName, item.DisplayName)
  137. {
  138. IsDisabled = item.IsDisabled,
  139. IsHidden = item.IsHidden,
  140. IsReadOnly = item.IsReadonly,
  141. IsOnlyView = item.IsOnlyView,
  142. FieldType = item.FieldType,
  143. ExpType = item.ExpType,
  144. };
  145. if (item.SelectStr.NotEmpty())
  146. {
  147. s.WithSelect(item.SelectStr, false, item.IsSelectTree);
  148. }
  149. search.Add(s);
  150. }
  151. return new VmSearch(search, ModalId + "_search");
  152. ;
  153. }
  154. return null;
  155. }
  156. }
  157. }
  158. public class VmQueryItem : VmTableItem
  159. {
  160. public VmQueryItem(
  161. string filedName,
  162. string displayName,
  163. bool isSearch = false,
  164. bool sort = false,
  165. int tipLength = 0,
  166. bool isDisabled = false,
  167. bool isHidden = false,
  168. FType fieldType = FType.S,
  169. EType expType = EType.Contains,
  170. string formatter = "")
  171. : base(filedName, displayName, formatter, sort, tipLength)
  172. {
  173. IsSearch = isSearch;
  174. IsDisabled = isDisabled;
  175. IsHidden = isHidden;
  176. FieldType = fieldType;
  177. ExpType = expType;
  178. }
  179. public bool IsSearch { get; set; }
  180. public bool IsHidden { get; set; }
  181. public bool IsFieldHidden { get; set; }
  182. public bool IsOnlyView { get; set; }
  183. public bool IsDisabled { get; set; }
  184. public bool IsReadonly { get; set; }
  185. public string DisabledStr => IsDisabled ? "disable" : "";
  186. public bool IsSelectTree { get; set; }
  187. public string SelectStr { get; set; }
  188. public FType FieldType { get; set; }
  189. public EType ExpType { get; set; }
  190. //public string FiledName { get; set; }
  191. //public string DisplayName { get; set; }
  192. //public string Formatter { get; set; }
  193. //public string FormatterStr => string.IsNullOrEmpty(Formatter) ? "" : "data-formatter=\"" + Formatter + "\"";
  194. //public string ClassName { get; set; }
  195. //public int? Width { get; set; }
  196. //public string WidthUnit { get; set; }
  197. //public string WidthStr => Width != null ? $" data-width=\"{Width}{WidthUnit}\"" : "";
  198. //public bool IsSort { get; set; }
  199. //public string Sort => IsSort ? " data-sortable=\"true\"" : "";
  200. //private int TipLength { get; set; }
  201. //public string Tip => TipLength > 0 ? $"data-tip=\"{TipLength}\"" : "";
  202. public new VmQueryItem WithWidth(int width, string unit = "px")
  203. {
  204. Width = width;
  205. WidthUnit = unit;
  206. return this;
  207. }
  208. /// <summary>
  209. /// 超过指定字符将 被截取 用tip的方式显示
  210. /// </summary>
  211. /// <param name="showLength">默认 30个字符</param>
  212. /// <returns></returns>
  213. public new VmQueryItem WithTip(int showLength = 30)
  214. {
  215. TipLength = showLength;
  216. return this;
  217. }
  218. public new VmQueryItem WithSort()
  219. {
  220. IsSort = true;
  221. return this;
  222. }
  223. public new VmQueryItem WithClassName(string className)
  224. {
  225. ClassName = className;
  226. return this;
  227. }
  228. public new VmQueryItem WithFormatter(string formatter)
  229. {
  230. Formatter = formatter;
  231. return this;
  232. }
  233. public VmQueryItem WithHidden()
  234. {
  235. IsHidden = true;
  236. return this;
  237. }
  238. public VmQueryItem WithDisabled()
  239. {
  240. IsDisabled = true;
  241. return this;
  242. }
  243. public VmQueryItem WithFieldType(FType fieldType, EType expType = EType.Contains)
  244. {
  245. FieldType = fieldType;
  246. ExpType = expType;
  247. return this;
  248. }
  249. public VmQueryItem WithExpType(EType expType)
  250. {
  251. ExpType = expType;
  252. return this;
  253. }
  254. public VmQueryItem WithSelectStr(string str, bool isSelectTree = false)
  255. {
  256. IsSelectTree = isSelectTree;
  257. SelectStr = str;
  258. return this;
  259. }
  260. public VmQueryItem WithSelectStr(Dictionary<string, string> dic, bool isSelectTree = false)
  261. {
  262. IsSelectTree = isSelectTree;
  263. if (dic.ContainsKey(FiledName)) SelectStr = dic[FiledName];
  264. return this;
  265. }
  266. public VmQueryItem WithOnlyView()
  267. {
  268. IsOnlyView = true;
  269. return this;
  270. }
  271. public VmQueryItem WithFieldHidden()
  272. {
  273. IsFieldHidden = true;
  274. IsSearch = true;
  275. return this;
  276. }
  277. public VmQueryItem WithSearch(bool isHidden = true, bool isDisabled = false, bool isReadonly = false, bool isOnlyView = false)
  278. {
  279. IsSearch = true;
  280. IsHidden = isHidden;
  281. IsDisabled = isDisabled;
  282. IsReadonly = isReadonly;
  283. IsOnlyView = isOnlyView;
  284. return this;
  285. }
  286. }
  287. public class VmQueryTreeSearch
  288. {
  289. public string Field { get; set; }
  290. public int FType { get; set; }
  291. public int EType { get; set; }
  292. public string SelectUrl { get; set; }
  293. public string SelectFieldName { get; set; }
  294. }