123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Linq.Expressions;
- namespace Bowin.Web.Controls.Mvc
- {
- public static class UserSelectorExtensions
- {
- public static MvcHtmlString UserSelector(this System.Web.Mvc.HtmlHelper htmlHelper, UserSelectorOptions options)
- {
- Dictionary<string, string> dictProperty = new Dictionary<string, string>();
- List<string> lstClass = new List<string>();
- Dictionary<string, string> dictStyle = new Dictionary<string, string>();
- Dictionary<string, string> dictOption = new Dictionary<string, string>();
- lstClass.Add("easyui-triggerbox");
- dictProperty.Add("type", "text");
- if (!string.IsNullOrEmpty(options.ID))
- {
- dictProperty.Add("id", options.ID);
- }
- if (!string.IsNullOrEmpty(options.Name))
- {
- dictProperty.Add("name", options.Name);
- }
- if (!string.IsNullOrEmpty(options.CssClass))
- {
- lstClass.Add(options.CssClass);
- }
- if (options.Width.HasValue)
- {
- if (options.Width < 1)
- {
- dictStyle.Add("width", options.Width.Value.ToString("#.##%"));
- }
- else
- {
- dictStyle.Add("width", options.Width.Value + "px");
- }
- }
- //{ field: 'UserID', title: 'UserID', checkbox: true },
- // { field: 'DepartmentName', title: '部门机构' },
- // { field: 'LoginName', title: '登录名' },
- // { field: 'UserName', title: '用户名' }
- UrlHelper Url = new UrlHelper(htmlHelper.ViewContext.RequestContext);
- var defualtUserInfo = "[";
- if (options.DefaultUserInfo.Count > 0)
- {
- foreach (var key in options.DefaultUserInfo.Keys)
- {
- var values = options.DefaultUserInfo[key];
- defualtUserInfo = string.Concat(defualtUserInfo
- , "{'LoginID':'", values[0]
- , "','text':'", values[1]
- , "','value':'", values[2]
- , "','DepartmentName':'", values[3]
- , "','DepartmentID':'", values[4], "'},");
- }
- defualtUserInfo = defualtUserInfo.TrimEnd(',');
- }
- defualtUserInfo = string.Concat(defualtUserInfo, "]");
- var uniqueID = Guid.NewGuid().ToString().Replace("-", "");
- if (String.IsNullOrEmpty(options.afterSelectScript))
- {
- options.afterSelectScript = "function(){}";
- }
- dictOption.Add("onSearch", string.Concat("function () { var _this = this;SelectUserContorl('', ",
- (!options.IsSingleSelect).ToString().ToLower(), ", ",
- "function (node) { ",
- // "var datas = []; ",
- //"$.each(node.rows, function () {",
- //" datas.push([{ 'text': this.UserName, 'value': this.UserID}][0]); });",
- "var opts = $(_this).triggerbox('options');opts.data=node.rows;",
- " $(_this).triggerbox('setValue', opts.data); },'",
- Url.Content("~/").TrimEnd('/'), "'", "," + options.afterSelectScript + ",this).show(); } "));
- dictOption.Add("separator", "';'");
- //dictOption.Add("data", "[" + string.Join(",", options.Data.Select(x => "{'text': '" + x.Value + "', 'value': '" + x.Key + "'}")) + "]");
- dictOption.Add("data", defualtUserInfo);
- //dictOption.Add("clear", "function (jq) {jq.each(function () {var opts = $(this).triggerbox(\"options\");opts.data = [];$(this).triggerbox(\"setValue\", opts.data);});}");
- if (options.IsEnabled.HasValue)
- {
- dictProperty.Add("editable", "'" + options.IsEnabled.ToString().ToLower() + "'");
- }
- dictProperty.Add("class", string.Join(" ", lstClass));
- dictProperty.Add("style", string.Join(";", dictStyle.Select(x => string.Concat(x.Key, ":", x.Value))));
- dictProperty.Add("data-options", string.Join(",", dictOption.Select(x => string.Concat(x.Key, ":", x.Value))));
- string html = string.Format("<input {0} />", string.Join(" ", dictProperty.Select(x => string.Concat(x.Key, "=", "\"", x.Value, "\""))));
- return MvcHtmlString.Create(html);
- }
- }
- public class UserSelectorOptions : BaseControlOptions
- {
- public UserSelectorOptions()
- {
- IsSingleSelect = false;
- }
- public Dictionary<string, string[]> DefaultUserInfo { get; set; }
- public double? Width { get; set; }
- public bool IsSingleSelect { get; set; }
- public Dictionary<string, object> Data { get; set; }
- public string afterSelectScript { get; set; }
- }
- }
|