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 GridAttributes { get; set; } public static ComboGrid CreateControl(ComboGridOptions comboGridOptions, IDictionary attributes = null, IDictionary 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 GetPropertyList() { var dictPropertyList = new Dictionary(); 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(); 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(GetPropertyList()); if (this.Attributes != null) { tabBuilder.MergeAttributes(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(""); html.Append(tagHidden.ToString(TagRenderMode.SelfClosing)); html.Append("
"); if (this.ComboGridOptions.GridOptions.Columns.Count <= 1) { this.ComboGridOptions.GridOptions.IsShowHeader = false; } html.Append(DataGrid.CreateControl(this.ComboGridOptions.GridOptions, this.GridAttributes).Render()); html.Append("
"); html.Append(tabBuilder.ToString(TagRenderMode.EndTag)); return html.ToString(); } } }