using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Bowin.Web.Controls.Mvc { public static class SelectorExtensions { public static MvcHtmlString Selector(this System.Web.Mvc.HtmlHelper htmlHelper, SelectorOptions 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("-", ""); string eventName = !string.IsNullOrEmpty(options.SelectedEvent) ? "(node,_this);" : ""; options.SelectedEvent += eventName; string eventBlock = !string.IsNullOrEmpty(options.SelectingEvent) ? "if(" + options.SelectingEvent + "()){return false;}" : ""; options.SelectingEvent = eventBlock; string QueryUrlFun = !string.IsNullOrEmpty(options.QueryUrlFun) ? options.QueryUrlFun : "'" + options.DataUrl + "'"; options.QueryUrlFun = QueryUrlFun; string GuidFun = !string.IsNullOrEmpty(options.GuidFun) ? options.GuidFun : "'" + Guid.NewGuid() + "'"; options.GuidFun = GuidFun; if (!string.IsNullOrEmpty(options.OnDelete)) { dictOption.Add("onDelete", options.OnDelete); } dictOption.Add("onSearch", string.Concat("function () { " + options.SelectingEvent + " var _this = this; new Selector(" + options.GuidFun + ",'" + options.Title + "','" + options.Colums + "'," + options.QueryUrlFun + ",'" + (options.QueryParams ?? "{}") + "', " + (!options.IsSingleSelect).ToString().ToLower() + ", function (node) { var datas = []; $.each(node.rows, function () { datas.push([{ 'text': this." + options.DataText + ", 'value': this." + options.DataValue + "}][0]); }); $(_this).triggerbox('setValue', datas);" + (options.SelectedEvent) + " },function(){return {selectRows:$(_this).triggerbox('getValue'),valueField:'" + options.DataValue + "'};}," + (options.IsAsyncPage ? "true" : "false") + ").show(); } ")); dictOption.Add("separator", "';'"); dictOption.Add("required", options.Required ? "true" : "false"); dictOption.Add("enable", options.IsEnabled == false ? "false" : "true"); dictOption.Add("data", "[" + string.Join(",", options.Data.Select(x => "{'text': '" + x.Value + "', 'value': '" + x.Key + "'}")) + "]"); dictOption.Add("dataUrl", options.Autocomplete ? options.QueryUrlFun : "''"); dictOption.Add("dataText", "'" + options.DataText + "'"); dictOption.Add("dataValue", "'" + options.DataValue + "'"); dictOption.Add("isSingle", (options.IsSingleSelect).ToString().ToLower()); dictOption.Add("autocomplete", (options.Autocomplete).ToString().ToLower()); dictOption.Add("selectedEvent", "function(node,_this){" + options.SelectedEvent + "}"); 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 SelectorOptions : BaseControlOptions { public SelectorOptions() { IsSingleSelect = false; Autocomplete = false; Data = new Dictionary(); IsAsyncPage = false; } public string DataUrl { get; set; } public double? Width { get; set; } public bool IsSingleSelect { get; set; } public bool Autocomplete { get; set; } public Dictionary Data { get; set; } /// /// 格式:列标题:绑定字段:列宽。如>Key:ProjectID:100,项目名称:ProjectName,项目编号:ProjectNumber:150 /// 主键用Key标识 /// public string Colums { get; set; } /// /// 数据提供方法需要参数格式如>id:2,name:zhangsan /// public string QueryParams { get; set; } /// /// 弹出窗口显示标题 /// public string Title { get; set; } /// /// 用于选择后在文本框显示的文本 /// public string DataText { get; set; } /// /// 用于选择后在文本框隐藏的值 /// public string DataValue { get; set; } /// /// 选择后调用的JS方法 /// public string SelectedEvent { get; set; } /// /// 选择前调用的方法 /// public string SelectingEvent { get; set; } /// /// 动态获取查询参数 /// public string QueryUrlFun { get; set; } /// /// 动态设置选择控件的唯一值 /// public string GuidFun { get; set; } /// /// 删除事件 /// public string OnDelete { get; set; } /// /// 是否动态加载分页数据 /// public bool IsAsyncPage { get; set; } } /// /// 站点信息 选项重载 /// public class SelectorSiteOptions : SelectorOptions { public SelectorSiteOptions() { ID = "Site"; Name = "Site"; IsSingleSelect = true; DataUrl = "/Comm/GetSiteList"; QueryParams = "id:1"; TextName = "SiteName"; Title = "站点基础表"; Colums = "Key:ID,站点名称:SiteName:100:query,站点属性:TypeName:100,所在镇区:TownName:100,规划站名:PlanningName:100,规划期数:PlanningNum:100"; DataValue = "ID"; DataText = "SiteName"; SelectedEvent = "selectEvent"; } } }