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(); SelectedValues = new List(); } 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 SelectedValues { get; set; } public List ItemList { get; private set; } public string ItemSourceUrl { get; set; } public string SelectedValueUrl { get; set; } public string TextField { get; set; } public string ValueField { get; set; } /// /// 是否显示 /// public bool? IsShow { 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_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(); 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(GetPropertyList()); if (this.Attributes != null) { tabBuilder.MergeAttributes(this.Attributes); } //StringBuilder checkListHtml = new StringBuilder(); //if (ItemList.Count > 0) //{ // int pages = Convert.ToInt32( // Math.Ceiling((Double)ItemList.Count / this.columnCount) // ); // checkListHtml.Append(""); // for (int i = 0; i < pages; i ++) // { // checkListHtml.Append(""); // for (int j = 0; j < this.columnCount; j ++) // { // int curIndex = (i * this.columnCount + j); // if (ItemList.Count > curIndex) // { // checkListHtml.Append(""); // } // else // { // checkListHtml.Append(""); // } // } // checkListHtml.Append(""); // } // checkListHtml.Append("
"); // checkListHtml.AppendFormat("{2}", // this.Name, // ItemList[curIndex].Value, // ItemList[curIndex].Text, // ((this.SelectedValues.Contains(ItemList[curIndex].Value)) ? "checked" : "") // ); // checkListHtml.Append(" 
"); // tabBuilder.InnerHtml = checkListHtml.ToString(); //} return tabBuilder.ToString(TagRenderMode.Normal); } public static CheckList CreateControl(ListControlOptions listControlOptions, IDictionary 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; } } }