123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 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<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.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("<input {0} />", 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<string, object>();
- IsAsyncPage = false;
- }
- public string DataUrl { get; set; }
- public double? Width { get; set; }
- public bool IsSingleSelect { get; set; }
- public bool Autocomplete { get; set; }
- public Dictionary<string, object> Data { get; set; }
- /// <summary>
- /// 格式:列标题:绑定字段:列宽。如>Key:ProjectID:100,项目名称:ProjectName,项目编号:ProjectNumber:150
- /// 主键用Key标识
- /// </summary>
- public string Colums { get; set; }
- /// <summary>
- /// 数据提供方法需要参数格式如>id:2,name:zhangsan
- /// </summary>
- public string QueryParams { get; set; }
- /// <summary>
- /// 弹出窗口显示标题
- /// </summary>
- public string Title { get; set; }
- /// <summary>
- /// 用于选择后在文本框显示的文本
- /// </summary>
- public string DataText { get; set; }
- /// <summary>
- /// 用于选择后在文本框隐藏的值
- /// </summary>
- public string DataValue { get; set; }
- /// <summary>
- /// 选择后调用的JS方法
- /// </summary>
- public string SelectedEvent { get; set; }
- /// <summary>
- /// 选择前调用的方法
- /// </summary>
- public string SelectingEvent { get; set; }
- /// <summary>
- /// 动态获取查询参数
- /// </summary>
- public string QueryUrlFun { get; set; }
- /// <summary>
- /// 动态设置选择控件的唯一值
- /// </summary>
- public string GuidFun { get; set; }
- /// <summary>
- /// 删除事件
- /// </summary>
- public string OnDelete { get; set; }
- /// <summary>
- /// 是否动态加载分页数据
- /// </summary>
- public bool IsAsyncPage { get; set; }
- }
- /// <summary>
- /// 站点信息 选项重载
- /// </summary>
- 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";
- }
- }
- }
|