123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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<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("-", "");
- //区分是否是函数动态获取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("<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;
- Data = new Dictionary<string, object>();
- }
- public string ControlID { get; set; }
- public double? Width { get; set; }
- public bool IsSingleSelect { get; set; }
- public Dictionary<string, object> Data { get; set; }
- public string TreeDataUrl { get; set; }
- public string DatagridDataUrl { get; set; }
- /// <summary>
- /// 是否特定用户选择
- /// </summary>
- public string isSpecific { get; set; }
- /// <summary>
- /// 选择后调用的JS方法
- /// </summary>
- public string SelectedEvent { get; set; }
- /// <summary>
- /// 删除事件
- /// </summary>
- public string OnDelete { get; set; }
- /// <summary>
- /// 公司类型
- /// </summary>
- public string CompanyType { get; set; }
- /// <summary>
- /// 动态设置选择控件的唯一值
- /// </summary>
- public string GuidFun { get; set; }
- }
- }
|