SelectorExtensions.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. namespace Bowin.Web.Controls.Mvc
  7. {
  8. public static class SelectorExtensions
  9. {
  10. public static MvcHtmlString Selector(this System.Web.Mvc.HtmlHelper htmlHelper, SelectorOptions options)
  11. {
  12. Dictionary<string, string> dictProperty = new Dictionary<string, string>();
  13. List<string> lstClass = new List<string>();
  14. Dictionary<string, string> dictStyle = new Dictionary<string, string>();
  15. Dictionary<string, string> dictOption = new Dictionary<string, string>();
  16. lstClass.Add("easyui-triggerbox");
  17. dictProperty.Add("type", "text");
  18. if (!string.IsNullOrEmpty(options.ID))
  19. {
  20. dictProperty.Add("id", options.ID);
  21. }
  22. if (!string.IsNullOrEmpty(options.Name))
  23. {
  24. dictProperty.Add("name", options.Name);
  25. }
  26. if (!string.IsNullOrEmpty(options.TextName))
  27. {
  28. dictProperty.Add("textName", options.TextName);
  29. }
  30. if (!string.IsNullOrEmpty(options.CssClass))
  31. {
  32. lstClass.Add(options.CssClass);
  33. }
  34. if (options.Width.HasValue)
  35. {
  36. if (options.Width < 1)
  37. {
  38. dictStyle.Add("width", options.Width.Value.ToString("#.##%"));
  39. }
  40. else
  41. {
  42. dictStyle.Add("width", options.Width.Value + "px");
  43. }
  44. }
  45. var uniqueID = Guid.NewGuid().ToString().Replace("-", "");
  46. string eventName = !string.IsNullOrEmpty(options.SelectedEvent) ? "(node,_this);" : "";
  47. options.SelectedEvent += eventName;
  48. string eventBlock = !string.IsNullOrEmpty(options.SelectingEvent) ? "if(" + options.SelectingEvent + "()){return false;}" : "";
  49. options.SelectingEvent = eventBlock;
  50. string QueryUrlFun = !string.IsNullOrEmpty(options.QueryUrlFun) ? options.QueryUrlFun : "'" + options.DataUrl + "'";
  51. options.QueryUrlFun = QueryUrlFun;
  52. string GuidFun = !string.IsNullOrEmpty(options.GuidFun) ? options.GuidFun : "'" + Guid.NewGuid() + "'";
  53. options.GuidFun = GuidFun;
  54. if (!string.IsNullOrEmpty(options.OnDelete))
  55. {
  56. dictOption.Add("onDelete", options.OnDelete);
  57. }
  58. dictOption.Add("onSearch", string.Concat("function () { " + options.SelectingEvent + " var _this = this; new Selector(" + options.GuidFun + ",'" + options.Title + "','" + options.Colums + "'," + options.QueryUrlFun + ",'" + (options.QueryParams ?? "{}") + "', " + (!options.IsSingleSelect).ToString().ToLower() + ", function (node) { var datas = []; $.each(node.rows, function () { datas.push([{ 'text': this." + options.DataText + ", 'value': this." + options.DataValue + "}][0]); }); $(_this).triggerbox('setValue', datas);" + (options.SelectedEvent) + " },function(){return {selectRows:$(_this).triggerbox('getValue'),valueField:'" + options.DataValue + "'};}," + (options.IsAsyncPage ? "true" : "false") + ").show(); } "));
  59. dictOption.Add("separator", "';'");
  60. dictOption.Add("required", options.Required ? "true" : "false");
  61. dictOption.Add("enable", options.IsEnabled == false ? "false" : "true");
  62. dictOption.Add("data", "[" + string.Join(",", options.Data.Select(x => "{'text': '" + x.Value + "', 'value': '" + x.Key + "'}")) + "]");
  63. dictOption.Add("dataUrl", options.Autocomplete ? options.QueryUrlFun : "''");
  64. dictOption.Add("dataText", "'" + options.DataText + "'");
  65. dictOption.Add("dataValue", "'" + options.DataValue + "'");
  66. dictOption.Add("isSingle", (options.IsSingleSelect).ToString().ToLower());
  67. dictOption.Add("autocomplete", (options.Autocomplete).ToString().ToLower());
  68. dictOption.Add("selectedEvent", "function(node,_this){" + options.SelectedEvent + "}");
  69. dictProperty.Add("class", string.Join(" ", lstClass));
  70. dictProperty.Add("style", string.Join(";", dictStyle.Select(x => string.Concat(x.Key, ":", x.Value))));
  71. dictProperty.Add("data-options", string.Join(",", dictOption.Select(x => string.Concat(x.Key, ":", x.Value))));
  72. string html = string.Format("<input {0} />", string.Join(" ", dictProperty.Select(x => string.Concat(x.Key, "=", "\"", x.Value, "\""))));
  73. return MvcHtmlString.Create(html);
  74. }
  75. }
  76. public class SelectorOptions : BaseControlOptions
  77. {
  78. public SelectorOptions()
  79. {
  80. IsSingleSelect = false;
  81. Autocomplete = false;
  82. Data = new Dictionary<string, object>();
  83. IsAsyncPage = false;
  84. }
  85. public string DataUrl { get; set; }
  86. public double? Width { get; set; }
  87. public bool IsSingleSelect { get; set; }
  88. public bool Autocomplete { get; set; }
  89. public Dictionary<string, object> Data { get; set; }
  90. /// <summary>
  91. /// 格式:列标题:绑定字段:列宽。如>Key:ProjectID:100,项目名称:ProjectName,项目编号:ProjectNumber:150
  92. /// 主键用Key标识
  93. /// </summary>
  94. public string Colums { get; set; }
  95. /// <summary>
  96. /// 数据提供方法需要参数格式如>id:2,name:zhangsan
  97. /// </summary>
  98. public string QueryParams { get; set; }
  99. /// <summary>
  100. /// 弹出窗口显示标题
  101. /// </summary>
  102. public string Title { get; set; }
  103. /// <summary>
  104. /// 用于选择后在文本框显示的文本
  105. /// </summary>
  106. public string DataText { get; set; }
  107. /// <summary>
  108. /// 用于选择后在文本框隐藏的值
  109. /// </summary>
  110. public string DataValue { get; set; }
  111. /// <summary>
  112. /// 选择后调用的JS方法
  113. /// </summary>
  114. public string SelectedEvent { get; set; }
  115. /// <summary>
  116. /// 选择前调用的方法
  117. /// </summary>
  118. public string SelectingEvent { get; set; }
  119. /// <summary>
  120. /// 动态获取查询参数
  121. /// </summary>
  122. public string QueryUrlFun { get; set; }
  123. /// <summary>
  124. /// 动态设置选择控件的唯一值
  125. /// </summary>
  126. public string GuidFun { get; set; }
  127. /// <summary>
  128. /// 删除事件
  129. /// </summary>
  130. public string OnDelete { get; set; }
  131. /// <summary>
  132. /// 是否动态加载分页数据
  133. /// </summary>
  134. public bool IsAsyncPage { get; set; }
  135. }
  136. /// <summary>
  137. /// 站点信息 选项重载
  138. /// </summary>
  139. public class SelectorSiteOptions : SelectorOptions
  140. {
  141. public SelectorSiteOptions()
  142. {
  143. ID = "Site";
  144. Name = "Site";
  145. IsSingleSelect = true;
  146. DataUrl = "/Comm/GetSiteList";
  147. QueryParams = "id:1";
  148. TextName = "SiteName";
  149. Title = "站点基础表";
  150. Colums = "Key:ID,站点名称:SiteName:100:query,站点属性:TypeName:100,所在镇区:TownName:100,规划站名:PlanningName:100,规划期数:PlanningNum:100";
  151. DataValue = "ID";
  152. DataText = "SiteName";
  153. SelectedEvent = "selectEvent";
  154. }
  155. }
  156. }