UserSelectorExtensions.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Linq.Expressions;
  7. namespace Bowin.Web.Controls.Mvc
  8. {
  9. public static class UserSelectorExtensions
  10. {
  11. public static MvcHtmlString UserSelector(this System.Web.Mvc.HtmlHelper htmlHelper, UserSelectorOptions options)
  12. {
  13. Dictionary<string, string> dictProperty = new Dictionary<string, string>();
  14. List<string> lstClass = new List<string>();
  15. Dictionary<string, string> dictStyle = new Dictionary<string, string>();
  16. Dictionary<string, string> dictOption = new Dictionary<string, string>();
  17. lstClass.Add("easyui-triggerbox");
  18. dictProperty.Add("type", "text");
  19. if (!string.IsNullOrEmpty(options.ID))
  20. {
  21. dictProperty.Add("id", options.ID);
  22. }
  23. if (!string.IsNullOrEmpty(options.Name))
  24. {
  25. dictProperty.Add("name", options.Name);
  26. }
  27. if (!string.IsNullOrEmpty(options.CssClass))
  28. {
  29. lstClass.Add(options.CssClass);
  30. }
  31. if (options.Width.HasValue)
  32. {
  33. if (options.Width < 1)
  34. {
  35. dictStyle.Add("width", options.Width.Value.ToString("#.##%"));
  36. }
  37. else
  38. {
  39. dictStyle.Add("width", options.Width.Value + "px");
  40. }
  41. }
  42. //{ field: 'UserID', title: 'UserID', checkbox: true },
  43. // { field: 'DepartmentName', title: '部门机构' },
  44. // { field: 'LoginName', title: '登录名' },
  45. // { field: 'UserName', title: '用户名' }
  46. UrlHelper Url = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  47. var defualtUserInfo = "[";
  48. if (options.DefaultUserInfo.Count > 0)
  49. {
  50. foreach (var key in options.DefaultUserInfo.Keys)
  51. {
  52. var values = options.DefaultUserInfo[key];
  53. defualtUserInfo = string.Concat(defualtUserInfo
  54. , "{'LoginID':'", values[0]
  55. , "','text':'", values[1]
  56. , "','value':'", values[2]
  57. , "','DepartmentName':'", values[3]
  58. , "','DepartmentID':'", values[4], "'},");
  59. }
  60. defualtUserInfo = defualtUserInfo.TrimEnd(',');
  61. }
  62. defualtUserInfo = string.Concat(defualtUserInfo, "]");
  63. var uniqueID = Guid.NewGuid().ToString().Replace("-", "");
  64. if (String.IsNullOrEmpty(options.afterSelectScript))
  65. {
  66. options.afterSelectScript = "function(){}";
  67. }
  68. dictOption.Add("onSearch", string.Concat("function () { var _this = this;SelectUserContorl('', ",
  69. (!options.IsSingleSelect).ToString().ToLower(), ", ",
  70. "function (node) { ",
  71. // "var datas = []; ",
  72. //"$.each(node.rows, function () {",
  73. //" datas.push([{ 'text': this.UserName, 'value': this.UserID}][0]); });",
  74. "var opts = $(_this).triggerbox('options');opts.data=node.rows;",
  75. " $(_this).triggerbox('setValue', opts.data); },'",
  76. Url.Content("~/").TrimEnd('/'), "'", "," + options.afterSelectScript + ",this).show(); } "));
  77. dictOption.Add("separator", "';'");
  78. //dictOption.Add("data", "[" + string.Join(",", options.Data.Select(x => "{'text': '" + x.Value + "', 'value': '" + x.Key + "'}")) + "]");
  79. dictOption.Add("data", defualtUserInfo);
  80. //dictOption.Add("clear", "function (jq) {jq.each(function () {var opts = $(this).triggerbox(\"options\");opts.data = [];$(this).triggerbox(\"setValue\", opts.data);});}");
  81. if (options.IsEnabled.HasValue)
  82. {
  83. dictProperty.Add("editable", "'" + options.IsEnabled.ToString().ToLower() + "'");
  84. }
  85. dictProperty.Add("class", string.Join(" ", lstClass));
  86. dictProperty.Add("style", string.Join(";", dictStyle.Select(x => string.Concat(x.Key, ":", x.Value))));
  87. dictProperty.Add("data-options", string.Join(",", dictOption.Select(x => string.Concat(x.Key, ":", x.Value))));
  88. string html = string.Format("<input {0} />", string.Join(" ", dictProperty.Select(x => string.Concat(x.Key, "=", "\"", x.Value, "\""))));
  89. return MvcHtmlString.Create(html);
  90. }
  91. }
  92. public class UserSelectorOptions : BaseControlOptions
  93. {
  94. public UserSelectorOptions()
  95. {
  96. IsSingleSelect = false;
  97. }
  98. public Dictionary<string, string[]> DefaultUserInfo { get; set; }
  99. public double? Width { get; set; }
  100. public bool IsSingleSelect { get; set; }
  101. public Dictionary<string, object> Data { get; set; }
  102. public string afterSelectScript { get; set; }
  103. }
  104. }