123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- 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<ListControlItem>();
- }
- 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<ListControlItem> ItemList
- {
- get;
- private set;
- }
- public string ItemSourceUrl { get; set; }
- public string TextField
- {
- get;
- set;
- }
- public string ValueField
- {
- get;
- set;
- }
- /// <summary>
- /// 是否启用
- /// </summary>
- public bool? IsEnabled { get; set; }
- public string OnClick { get; set; }
- public string OnLoadSuccess { get; set; }
- protected IDictionary<string, string> GetPropertyList()
- {
- var dictPropertyList = new Dictionary<string, string>();
- 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<string, string>();
- 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<string, string>(GetPropertyList());
- if (this.Attributes != null)
- {
- tabBuilder.MergeAttributes<string, string>(this.Attributes);
- }
- return tabBuilder.ToString(TagRenderMode.Normal);
- }
- public static QQList CreateControl(QQListOptions qqListOptions, IDictionary<string, string> 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;
- }
- }
- }
|