using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using System.Drawing; using Bowin.Common.Utility; namespace Bowin.Web.Controls.Mvc { public class DraggableList : BaseFormControl { public List ItemList { get; set; } public string ItemSourceUrl { get; set; } public int ItemHeight { get; set; } public string TitleField { get; set; } public string IconField { get; set; } public string ValueField { get; set; } public int? Width { get; set; } public int? Height { get; set; } public Color ForeColor { get; set; } public DraggableList() { ItemList = new List(); } protected IDictionary GetPropertyList() { var dictPropertyList = new Dictionary(); if (!string.IsNullOrEmpty(this.ID)) { dictPropertyList.Add("id", this.ID); } if (!string.IsNullOrEmpty(this.Name)) { dictPropertyList.Add("name", this.Name); } dictPropertyList.Add("class", "easyui-draggableList"); string style = ""; if (this.Width.HasValue) { style += "width: " + this.Width.Value.ToString() + "px;"; } else { style += "width: 100%;"; } if (this.Height.HasValue) { style += "height: " + this.Height.Value.ToString() + "px;"; } else { style += "height: 100%;"; } style += "overflow-x: scroll;"; dictPropertyList.Add("style", style); #region data-options var dictOptions = new Dictionary(); if (this.ForeColor != null) { dictOptions.Add("foreColor", "'#" + this.ForeColor.ToRGBString() + "'"); } if (!string.IsNullOrEmpty(this.ItemSourceUrl)) { dictOptions.Add("url", "'" + this.ItemSourceUrl + "'"); if (string.IsNullOrEmpty(this.TitleField)) { throw new Exception("指定了ItemSourceUrl就一定要设置TitleField"); } } if (!string.IsNullOrEmpty(this.TitleField)) { dictOptions.Add("titleField", "'" + this.TitleField + "'"); } if (!string.IsNullOrEmpty(this.ValueField)) { dictOptions.Add("valueField", "'" + this.ValueField + "'"); } if (!string.IsNullOrEmpty(this.IconField)) { dictOptions.Add("iconField", "'" + this.IconField + "'"); } dictOptions.Add("itemHeight", this.ItemHeight.ToString()); 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 DraggableList CreateControl(DraggableListControlOptions dragableListOptions, IDictionary attributes = null) { DraggableList dragableList = new DraggableList(); dragableList.ID = dragableListOptions.ID; if (dragableListOptions.ItemList != null) { dragableList.ItemList.AddRange(dragableListOptions.ItemList); } dragableList.Name = dragableListOptions.Name; dragableList.Attributes = attributes; dragableList.ItemSourceUrl = dragableListOptions.ItemSourceUrl; dragableList.ItemList = dragableListOptions.ItemList; dragableList.ItemHeight = dragableListOptions.ItemHeight; dragableList.TitleField = dragableListOptions.TitleField; dragableList.ValueField = dragableListOptions.ValueField; dragableList.IconField = dragableListOptions.IconField; dragableList.Width = dragableListOptions.Width; dragableList.Height = dragableListOptions.Height; dragableList.ForeColor = dragableListOptions.ForeColor; return dragableList; } } }