123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Mvc;
- namespace Bowin.Web.Controls.Mvc
- {
- public class CheckList : BaseControl
- {
- public CheckList()
- : base()
- {
- ItemList = new List<ListControlItem>();
- SelectedValues = new List<object>();
- }
- public int? Width { get; set; }
- public int? Height { get; set; }
- private int columnCount = 1;
- public int ColumnCount {
- get {
- return this.columnCount;
- }
- set
- {
- this.columnCount = value;
- }
- }
- public List<object> SelectedValues
- {
- get;
- set;
- }
- public List<ListControlItem> ItemList
- {
- get;
- private set;
- }
- public string ItemSourceUrl { get; set; }
- public string SelectedValueUrl { get; set; }
- public string TextField
- {
- get;
- set;
- }
- public string ValueField
- {
- get;
- set;
- }
- /// <summary>
- /// 是否显示
- /// </summary>
- public bool? IsShow { 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_checklist_" + 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-checkList {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
- if (this.Width.HasValue)
- {
- dictPropertyList.Add("width", this.Width.Value.ToString() + "px");
- }
- if (this.Height.HasValue)
- {
- dictPropertyList.Add("height", this.Height.Value.ToString() + "px");
- }
- else
- {
- dictPropertyList.Add("height", "auto");
- }
- #region data-options
- var dictOptions = new Dictionary<string, string>();
- if (!string.IsNullOrEmpty(this.Name))
- {
- dictOptions.Add("checkName", "'" + this.Name + "'");
- }
- else
- {
- dictOptions.Add("checkName", "'" + this.ID + "'");
- }
- dictOptions.Add("columnCount", this.ColumnCount.ToString());
- 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.IsShow.HasValue && this.IsShow == false)
- {
- dictOptions.Add("isShow", "false");
- }
- else
- {
- dictOptions.Add("isShow", "true");
- }
- 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 (this.SelectedValueUrl != null)
- {
- dictOptions.Add("valueUrl", "'" + this.SelectedValueUrl + "'");
- }
- 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))),
- "]"));
- }
- if (SelectedValues.Count > 0)
- {
- dictOptions.Add("values",
- string.Concat("[",
- string.Join(",", SelectedValues.Select(x => "'" + x.ToString() + "'")),
- "]"));
- }
- //dictOptions.Add("required", "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()
- {
- TagBuilder tabBuilder = new TagBuilder("div");
- tabBuilder.MergeAttributes<string, string>(GetPropertyList());
- if (this.Attributes != null)
- {
- tabBuilder.MergeAttributes<string, string>(this.Attributes);
- }
- //StringBuilder checkListHtml = new StringBuilder();
- //if (ItemList.Count > 0)
- //{
- // int pages = Convert.ToInt32(
- // Math.Ceiling((Double)ItemList.Count / this.columnCount)
- // );
- // checkListHtml.Append("<table border='0' width='100%'>");
- // for (int i = 0; i < pages; i ++)
- // {
- // checkListHtml.Append("<tr>");
- // for (int j = 0; j < this.columnCount; j ++)
- // {
- // int curIndex = (i * this.columnCount + j);
- // if (ItemList.Count > curIndex)
- // {
- // checkListHtml.Append("<td>");
- // checkListHtml.AppendFormat("<input name=\"{0}\" {3} type=checkbox value=\"{1}\"/>{2}",
- // this.Name,
- // ItemList[curIndex].Value,
- // ItemList[curIndex].Text,
- // ((this.SelectedValues.Contains(ItemList[curIndex].Value)) ? "checked" : "")
- // );
- // checkListHtml.Append("</td>");
- // }
- // else
- // {
- // checkListHtml.Append("<td> </td>");
- // }
- // }
- // checkListHtml.Append("</tr>");
- // }
- // checkListHtml.Append("</table>");
- // tabBuilder.InnerHtml = checkListHtml.ToString();
- //}
- return tabBuilder.ToString(TagRenderMode.Normal);
- }
- public static CheckList CreateControl(ListControlOptions listControlOptions, IDictionary<string, string> attributes = null)
- {
- CheckList checkList = new CheckList();
- checkList.CssClass = listControlOptions.CssClass;
- checkList.ID = listControlOptions.ID;
- checkList.ItemList.AddRange(listControlOptions.ItemList);
- checkList.Name = listControlOptions.Name;
- checkList.IsShow = listControlOptions.IsShow;
- checkList.IsEnabled = listControlOptions.IsEnabled;
- checkList.SelectedValues = listControlOptions.SelectedValues;
- checkList.ItemSourceUrl = listControlOptions.ItemSourceUrl;
- checkList.SelectedValueUrl = listControlOptions.SelectedValueUrl;
- checkList.TextField = string.IsNullOrWhiteSpace(listControlOptions.TextField) ? "Text" : listControlOptions.TextField;
- checkList.ValueField = string.IsNullOrWhiteSpace(listControlOptions.ValueField) ? "Value" : listControlOptions.ValueField;
- checkList.Width = listControlOptions.Width;
- checkList.ColumnCount = listControlOptions.ColumnCount;
- checkList.OnClick = listControlOptions.OnClick;
- checkList.OnLoadSuccess = listControlOptions.OnLoadSuccess;
- return checkList;
- }
- }
- }
|