using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; 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.TextName)) { dictProperty.Add("textName", options.TextName); } 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"); } } var uniqueID = Guid.NewGuid().ToString().Replace("-", ""); //区分是否是函数动态获取url还是直接使用url; var DatagridDataUrl = !string.IsNullOrEmpty(options.DatagridDataUrl) && options.DatagridDataUrl.Contains("(") ? options.DatagridDataUrl : "'" + options.DatagridDataUrl + "'"; string GuidFun = !string.IsNullOrEmpty(options.GuidFun) ? options.GuidFun : "'" + Guid.NewGuid() + "'"; options.GuidFun = GuidFun; string eventName = !string.IsNullOrEmpty(options.SelectedEvent) ? "(node);" : ""; options.SelectedEvent += eventName; if (!string.IsNullOrEmpty(options.OnDelete)) dictOption.Add("onDelete", options.OnDelete); 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(); } ")); dictOption.Add("separator", "';'"); dictOption.Add("required", options.Required ? "true" : "false"); dictOption.Add("enable", options.IsEnabled == false ? "false" : "true"); if (options.Data != null) dictOption.Add("data", "[" + string.Join(",", options.Data.Select(x => "{'text': '" + x.Value + "', 'value': '" + x.Key + "'}")) + "]"); 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; Data = new Dictionary(); } public string ControlID { get; set; } public double? Width { get; set; } public bool IsSingleSelect { get; set; } public Dictionary Data { get; set; } public string TreeDataUrl { get; set; } public string DatagridDataUrl { get; set; } /// /// 是否特定用户选择 /// public string isSpecific { get; set; } /// /// 选择后调用的JS方法 /// public string SelectedEvent { get; set; } /// /// 删除事件 /// public string OnDelete { get; set; } /// /// 公司类型 /// public string CompanyType { get; set; } /// /// 动态设置选择控件的唯一值 /// public string GuidFun { get; set; } } }