UserSelectorExtensions.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 UserSelectorExtensions
  9. {
  10. public static MvcHtmlString UserSelector(this System.Web.Mvc.HtmlHelper htmlHelper, UserSelectorOptions 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. //区分是否是函数动态获取url还是直接使用url;
  47. var DatagridDataUrl = !string.IsNullOrEmpty(options.DatagridDataUrl) && options.DatagridDataUrl.Contains("(") ? options.DatagridDataUrl : "'" + options.DatagridDataUrl + "'";
  48. string GuidFun = !string.IsNullOrEmpty(options.GuidFun) ? options.GuidFun : "'" + Guid.NewGuid() + "'";
  49. options.GuidFun = GuidFun;
  50. string eventName = !string.IsNullOrEmpty(options.SelectedEvent) ? "(node);" : "";
  51. options.SelectedEvent += eventName;
  52. if (!string.IsNullOrEmpty(options.OnDelete))
  53. dictOption.Add("onDelete", options.OnDelete);
  54. dictOption.Add("onSearch", string.Concat("function () { var _this = this; new SelectUserContorl(" + options.GuidFun + ",'', " + (!options.IsSingleSelect).ToString().ToLower() + ", function (node) { var datas = []; $.each(node, function () { datas.push([{ 'text': this.UserName, 'value': this.UserID}][0]); }); $(_this).triggerbox('setValue', datas); " + (options.SelectedEvent) + " },'" + options.TreeDataUrl + "'," + DatagridDataUrl + "," + (!string.IsNullOrEmpty(options.isSpecific) ? options.isSpecific : "false") + "," + (!string.IsNullOrEmpty(options.CompanyType) ? options.CompanyType : "''") + ").show(); } "));
  55. dictOption.Add("separator", "';'");
  56. dictOption.Add("required", options.Required ? "true" : "false");
  57. dictOption.Add("enable", options.IsEnabled == false ? "false" : "true");
  58. if (options.Data != null)
  59. dictOption.Add("data", "[" + string.Join(",", options.Data.Select(x => "{'text': '" + x.Value + "', 'value': '" + x.Key + "'}")) + "]");
  60. dictProperty.Add("class", string.Join(" ", lstClass));
  61. dictProperty.Add("style", string.Join(";", dictStyle.Select(x => string.Concat(x.Key, ":", x.Value))));
  62. dictProperty.Add("data-options", string.Join(",", dictOption.Select(x => string.Concat(x.Key, ":", x.Value))));
  63. string html = string.Format("<input {0} />", string.Join(" ", dictProperty.Select(x => string.Concat(x.Key, "=", "\"", x.Value, "\""))));
  64. return MvcHtmlString.Create(html);
  65. }
  66. }
  67. public class UserSelectorOptions : BaseControlOptions
  68. {
  69. public UserSelectorOptions()
  70. {
  71. IsSingleSelect = false;
  72. Data = new Dictionary<string, object>();
  73. }
  74. public string ControlID { get; set; }
  75. public double? Width { get; set; }
  76. public bool IsSingleSelect { get; set; }
  77. public Dictionary<string, object> Data { get; set; }
  78. public string TreeDataUrl { get; set; }
  79. public string DatagridDataUrl { get; set; }
  80. /// <summary>
  81. /// 是否特定用户选择
  82. /// </summary>
  83. public string isSpecific { get; set; }
  84. /// <summary>
  85. /// 选择后调用的JS方法
  86. /// </summary>
  87. public string SelectedEvent { get; set; }
  88. /// <summary>
  89. /// 删除事件
  90. /// </summary>
  91. public string OnDelete { get; set; }
  92. /// <summary>
  93. /// 公司类型
  94. /// </summary>
  95. public string CompanyType { get; set; }
  96. /// <summary>
  97. /// 动态设置选择控件的唯一值
  98. /// </summary>
  99. public string GuidFun { get; set; }
  100. }
  101. }