using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; namespace Bowin.Web.Controls.Mvc { public class QQList : BaseControl { public QQList() : base() { ItemList = new List(); } public int? Width { get; set; } public int? ItemWidth { get; set; } public int? ItemHeight { get; set; } private int columnCount = 1; public int ColumnCount { get { return this.columnCount; } set { this.columnCount = value; } } public List ItemList { get; private set; } public string ItemSourceUrl { get; set; } public string TextField { get; set; } public string ValueField { get; set; } /// /// 是否启用 /// public bool? IsEnabled { get; set; } public string OnClick { get; set; } public string OnLoadSuccess { get; set; } protected IDictionary GetPropertyList() { var dictPropertyList = new Dictionary(); if (string.IsNullOrEmpty(this.ID)) { this.ID = "dynamic_qqlist_" + DateTime.Now.ToString("yyyMMddHHmmssffff") + "_" + NewIDIndex(); } dictPropertyList.Add("id", this.ID); if (!string.IsNullOrEmpty(this.Name)) { dictPropertyList.Add("name", "div" + this.Name); } dictPropertyList.Add("class", string.Format("easyui-qqList {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass)); if (this.Width.HasValue) { if (this.Width.Value > 1) { dictPropertyList.Add("width", this.Width.Value.ToString() + "px"); } else { dictPropertyList.Add("width", (this.Width.Value * 100).ToString() + "%"); } } #region data-options var dictOptions = new Dictionary(); if (!string.IsNullOrEmpty(this.Name)) { dictOptions.Add("itemName", "'" + this.Name + "'"); } else { dictOptions.Add("itemName", "'" + this.ID + "'"); } dictOptions.Add("columnCount", this.ColumnCount.ToString()); if (this.ItemWidth.HasValue) { dictOptions.Add("itemWidth", this.ItemWidth.Value.ToString() + "px"); } else { dictOptions.Add("itemWidth", "32px"); } if (this.ItemHeight.HasValue) { dictOptions.Add("itemHeight", this.ItemHeight.Value.ToString() + "px"); } else { dictOptions.Add("itemHeight", "32px"); } if (!string.IsNullOrEmpty(this.ItemSourceUrl)) { dictOptions.Add("url", "'" + this.ItemSourceUrl + "'"); if (string.IsNullOrEmpty(this.TextField)) { throw new Exception("指定了ItemSourceUrl就一定要设置TextField"); } if (string.IsNullOrEmpty(this.ValueField)) { throw new Exception("指定了ItemSourceUrl就一定要设置ValueField"); } } if (this.IsEnabled.HasValue && this.IsEnabled == false) { dictOptions.Add("isEnabled", "false"); } else { dictOptions.Add("isEnabled", "true"); } if (!string.IsNullOrEmpty(this.TextField)) { dictOptions.Add("textField", "'" + this.TextField + "'"); } if (!string.IsNullOrEmpty(this.ValueField)) { dictOptions.Add("valueField", "'" + this.ValueField + "'"); } if (!string.IsNullOrEmpty(this.OnClick)) { dictOptions.Add("onclick", this.OnClick); } if (!string.IsNullOrEmpty(this.OnLoadSuccess)) { dictOptions.Add("onLoadSuccess", this.OnLoadSuccess); } if (ItemList.Count > 0) { dictOptions.Add("data", string.Concat("[", string.Join(",", ItemList.Select(x => string.Format("{{ {0}:'{1}',{2}:'{3}' }}", (string.IsNullOrEmpty(this.TextField) ? "Text" : this.TextField), x.Text, (string.IsNullOrEmpty(this.ValueField) ? "Value" : this.ValueField), x.Value))), "]")); } 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() { TagBuilder tabBuilder = new TagBuilder("div"); tabBuilder.MergeAttributes(GetPropertyList()); if (this.Attributes != null) { tabBuilder.MergeAttributes(this.Attributes); } return tabBuilder.ToString(TagRenderMode.Normal); } public static QQList CreateControl(QQListOptions qqListOptions, IDictionary attributes = null) { QQList qqList = new QQList(); qqList.CssClass = qqListOptions.CssClass; qqList.ID = qqListOptions.ID; qqList.ItemList.AddRange(qqListOptions.ItemList); qqList.Name = qqListOptions.Name; qqList.IsShow = qqListOptions.IsShow; qqList.IsEnabled = qqListOptions.IsEnabled; qqList.ItemSourceUrl = qqListOptions.ItemSourceUrl; qqList.TextField = string.IsNullOrWhiteSpace(qqListOptions.TextField) ? "Text" : qqListOptions.TextField; qqList.ValueField = string.IsNullOrWhiteSpace(qqListOptions.ValueField) ? "Value" : qqListOptions.ValueField; qqList.Width = qqListOptions.Width; qqList.ItemWidth = qqListOptions.ItemWidth; qqList.ItemHeight = qqListOptions.ItemHeight; qqList.ColumnCount = qqListOptions.ColumnCount; qqList.OnClick = qqListOptions.OnClick; qqList.OnLoadSuccess = qqListOptions.OnLoadSuccess; return qqList; } } }