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 dictProperty = new Dictionary(); List lstClass = new List(); Dictionary dictStyle = new Dictionary(); Dictionary dictOption = new Dictionary(); 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("", 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 DefaultUserInfo { get; set; } public double? Width { get; set; } public bool IsSingleSelect { get; set; } public Dictionary Data { get; set; } public string afterSelectScript { get; set; } } }