123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Mvc;
- namespace Bowin.Web.Controls.Mvc
- {
- public class ComboGrid : BaseControl
- {
- public ComboGridOptions ComboGridOptions { get; set; }
- public IDictionary<string, string> GridAttributes { get; set; }
- public static ComboGrid CreateControl(ComboGridOptions comboGridOptions, IDictionary<string, string> attributes = null, IDictionary<string, string> gridAttributes = null)
- {
- ComboGrid comboGrid = new ComboGrid();
- comboGrid.ComboGridOptions = comboGridOptions;
- comboGrid.ComboGridOptions.GridOptions.ID = comboGrid.ComboGridOptions.ID + "_gridX";
- comboGrid.ComboGridOptions.GridOptions.Name = comboGrid.ComboGridOptions.Name + "_gridX";
- comboGrid.ComboGridOptions.GridOptions.PagerType = PagerType.Simple;
- if (!comboGrid.ComboGridOptions.GridOptions.PageSize.HasValue)
- {
- comboGrid.ComboGridOptions.GridOptions.PageSize = 10;
- }
- comboGrid.Attributes = attributes;
- comboGrid.GridAttributes = gridAttributes;
- return comboGrid;
- }
- protected IDictionary<string, string> GetPropertyList()
- {
- var dictPropertyList = new Dictionary<string, string>();
- dictPropertyList.Add("id", this.ComboGridOptions.ID);
- if (!string.IsNullOrEmpty(this.ComboGridOptions.Name))
- {
- dictPropertyList.Add("comboname", this.ComboGridOptions.Name);
- }
- dictPropertyList.Add("class", string.Format("easyui-combogridX {0}", string.IsNullOrEmpty(this.ComboGridOptions.CssClass) ? "" : this.ComboGridOptions.CssClass));
- if (this.ComboGridOptions.Width.HasValue)
- {
- dictPropertyList.Add("width", this.ComboGridOptions.Width.ToString());
- }
- if (this.ComboGridOptions.IsShow.HasValue && !this.ComboGridOptions.IsShow.Value)
- {
- if (dictPropertyList.Keys.Contains("style"))
- dictPropertyList["style"] += "display:none;";
- else
- dictPropertyList.Add("style", "display:none;");
- }
- #region data-options
- var dictOptions = new Dictionary<string, string>();
- if (string.IsNullOrEmpty(this.ComboGridOptions.TextField))
- {
- throw new Exception("必须指定TextField");
- }
- if (string.IsNullOrEmpty(this.ComboGridOptions.ValueField))
- {
- throw new Exception("必须指定ValueField");
- }
- dictOptions.Add("textField", "'" + this.ComboGridOptions.TextField + "'");
- dictOptions.Add("valueField", "'" + this.ComboGridOptions.ValueField + "'");
- if (this.ComboGridOptions.IsAutoComplete.HasValue)
- {
- dictOptions.Add("isAutoComplete", this.ComboGridOptions.IsAutoComplete.Value ? "true" : "false");
- if (this.ComboGridOptions.MinReloadCharactor.HasValue)
- {
- dictOptions.Add("minReloadCharactor", this.ComboGridOptions.MinReloadCharactor.Value.ToString());
- }
- }
- if (!string.IsNullOrEmpty(this.ComboGridOptions.OnSelect))
- {
- dictOptions.Add("onSelect", this.ComboGridOptions.OnSelect);
- }
- if (!string.IsNullOrEmpty(this.ComboGridOptions.OnChange))
- {
- dictOptions.Add("onChange", this.ComboGridOptions.OnChange);
- }
- if (this.ComboGridOptions.Width.HasValue && !this.ComboGridOptions.PanelWidth.HasValue)
- {
- dictOptions.Add("panelWidth", Math.Ceiling((double)this.ComboGridOptions.Width.Value * 1.5).ToString());
- }
- else if (this.ComboGridOptions.PanelWidth.HasValue)
- {
- dictOptions.Add("panelWidth", this.ComboGridOptions.PanelWidth.ToString());
- }
- if (this.ComboGridOptions.Height.HasValue)
- {
- dictOptions.Add("panelHeight", Math.Ceiling((double)this.ComboGridOptions.Height.Value * 1.5).ToString());
- }
- if (ComboGridOptions.SelectedValue != null &&
- ComboGridOptions.SelectedValue.ToString() != "")
- {
- dictOptions.Add("value", "'" + this.ComboGridOptions.SelectedValue + "'");
- }
- else
- {
- dictOptions.Add("value", "'-1'");
- }
- if (!string.IsNullOrEmpty(ComboGridOptions.SelectedText))
- {
- dictOptions.Add("text", "'" + this.ComboGridOptions.SelectedText + "'");
- }
- else
- {
- dictOptions.Add("text", "''");
- }
- if (ComboGridOptions.SelectedIndex.HasValue)
- {
- dictOptions.Add("selectedIndex", this.ComboGridOptions.SelectedIndex.Value.ToString());
- }
- if (!string.IsNullOrEmpty(ComboGridOptions.EmptyText))
- {
- dictOptions.Add("emptyText", "'" + this.ComboGridOptions.EmptyText + "'");
- }
- if (ComboGridOptions.IsEnabled == false)
- {
- dictOptions.Add("enabled", "false");
- }
- else
- {
- dictOptions.Add("enabled", "true");
- }
- dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
- #endregion data-options
- return dictPropertyList;
- }
- public override string Render()
- {
- StringBuilder html = new StringBuilder();
- TagBuilder tabBuilder = new TagBuilder("span");
- tabBuilder.MergeAttributes<string, string>(GetPropertyList());
- if (this.Attributes != null)
- {
- tabBuilder.MergeAttributes<string, string>(this.Attributes);
- }
- TagBuilder tagInput = new TagBuilder("input");
- tagInput.AddCssClass("combo-text");
- if (this.ComboGridOptions.IsRequired == true)
- {
- tagInput.AddCssClass("validatebox-text");
- }
- tagInput.Attributes.Add("type", "text");
- if (!string.IsNullOrEmpty(this.ComboGridOptions.EmptyText))
- {
- //tagInput.Attributes.Add("placeholder", this.ComboGridOptions.EmptyText);
- tagInput.Attributes.Add("value", this.ComboGridOptions.EmptyText);
- tagInput.Attributes.Add("onfocus", "if (value =='" + this.ComboGridOptions.EmptyText + "'){value =''}");
- tagInput.Attributes.Add("onblur", "if (value ==''){value ='" + this.ComboGridOptions.EmptyText + "'}");
- }
- if (ComboGridOptions.IsEnabled == false)
- {
- tagInput.Attributes.Add("disabled", "disabled");
- }
- if ((this.ComboGridOptions.IsAutoComplete ?? true) == false)
- {
- tagInput.Attributes.Add("readonly", "true");
- }
- TagBuilder tagHidden = new TagBuilder("input");
- if (!string.IsNullOrEmpty(this.ComboGridOptions.Name))
- {
- tagHidden.Attributes.Add("name", this.ComboGridOptions.Name);
- }
- tagHidden.Attributes.Add("type", "hidden");
- tagHidden.AddCssClass("combogridX-value");
- html.Append(tabBuilder.ToString(TagRenderMode.StartTag));
- html.Append(tagInput.ToString(TagRenderMode.SelfClosing));
- html.Append("<span><span class=\"combo-clear\"></span><span class=\"combo-arrow combogridX-arrow\"></span></span>");
- html.Append(tagHidden.ToString(TagRenderMode.SelfClosing));
- html.Append("<div class=\"easyui-panel combogridX-expander\">");
- if (this.ComboGridOptions.GridOptions.Columns.Count <= 1)
- {
- this.ComboGridOptions.GridOptions.IsShowHeader = false;
- }
- html.Append(DataGrid.CreateControl(this.ComboGridOptions.GridOptions, this.GridAttributes).Render());
- html.Append("</div>");
- html.Append(tabBuilder.ToString(TagRenderMode.EndTag));
- return html.ToString();
- }
- }
- }
|