123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Mvc;
- using System.Configuration;
- namespace Bowin.Web.Controls.Mvc
- {
- public class DropdownList : BaseFormControl
- {
- public DropdownList()
- : base()
- {
- ItemList = new List<DropdownListItem>();
- SelectedValue = null;
- BindType = DropdownListBindType.None;
- }
- public int? Width { get; set; }
- public int? Height { get; set; }
- public bool? IsEditable { get; set; }
- public bool? IsMultiple { get; set; }
- public DropdownListBindType BindType { get; set; }
- public Dictionary<string, string> UrlParameters { get; set; }
- public static DropdownListItem SelectAllItem { get; set; }
- public static DropdownListItem PleaseSelectItem { get; set; }
- public static int SELECT_ALL { get; private set; }
- public static int PLEASE_SELECT { get; private set; }
- //public string Reload { get; set; }
- //public string ReloadSource { get; set; }
- static DropdownList()
- {
- SELECT_ALL = -1;
- PLEASE_SELECT = -1;
- SelectAllItem = new DropdownListItem { Text = "全部", Value = SELECT_ALL };
- PleaseSelectItem = new DropdownListItem { Text = "请选择", Value = PLEASE_SELECT };
- }
- public object SelectedValue
- {
- get;
- set;
- }
- public List<DropdownListItem> ItemList
- {
- get;
- private set;
- }
- public string ItemSourceUrl
- {
- get;
- set;
- }
- public string TextField
- {
- get;
- set;
- }
- public string ValueField
- {
- get;
- set;
- }
- /// <summary>
- /// 输出样式转换方法,如下
- /// function formatItem(row){
- /// var s = '<span style="font-weight:bold">' + row.text + '</span><br/>' +
- /// '<span style="color:#888">' + row.desc + '</span>';
- /// return s;
- ///}
- /// </summary>
- public string Formatter
- {
- get;
- set;
- }
- /// <summary>
- /// function(rec){
- /// var url = 'get_data2.php?id='+rec.id;
- /// $('#cc2').combobox('reload', url);
- /// }
- /// </summary>
- public string OnSelect
- {
- get;
- set;
- }
- public string OnUnselect { get; set; }
- public string OnChange { get; set; }
- public string OnLoadSuccess
- {
- get;
- set;
- }
- public string OnBeforeLoad { get; set; }
- protected IDictionary<string, string> GetPropertyList()
- {
- var dictPropertyList = new Dictionary<string, string>();
- if (!string.IsNullOrEmpty(this.ID))
- {
- dictPropertyList.Add("id", this.ID);
- }
- if (!string.IsNullOrEmpty(this.Name))
- {
- dictPropertyList.Add("name", this.Name);
- }
- if (this.IsShow.HasValue && !this.IsShow.Value)
- {
- if (dictPropertyList.Keys.Contains("style"))
- dictPropertyList["style"] += "display:none;";
- else
- dictPropertyList.Add("style", "display:none;");
- }
- if (this.Validator == null)
- {
- dictPropertyList.Add("class", string.Format("easyui-combobox {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
- }
- else
- {
- dictPropertyList.Add("class", string.Format("easyui-combobox {0} {1}", this.Validator.ValidatorClass,
- string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
- }
- //tabBuilder.MergeAttributes(this.Validator.GetOptions());
- //foreach (var attr in this.Attributes)
- //{
- // dictPropertyList.Add(attr.Key, attr.Value);
- //}
- #region data-options
- var dictOptions = new Dictionary<string, string>();
- if (!string.IsNullOrEmpty(this.ItemSourceUrl))
- {
- var url = "";
- var p = this.ItemSourceUrl.Split('?');
- if (p.Count() > 1)
- {
- url = p[0] + "?" + string.Join("&", new string[] { p[1], "bindType=" + (int)this.BindType });
- }
- else
- {
- url = this.ItemSourceUrl + "?bindType=" + (int)this.BindType;
- }
- if (this.UrlParameters != null && this.UrlParameters.Count > 0)
- {
- url +=
- string.Format(
- "{0}{1}",
- url.IndexOf("?") == -1 ? "?" : "&",
- string.Join("&", UrlParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)))
- );
- }
- dictOptions.Add("url", "'" + url + "'");
- if (string.IsNullOrEmpty(this.TextField))
- {
- throw new Exception("指定了ItemSourceUrl就一定要设置TextField");
- }
- if (string.IsNullOrEmpty(this.ValueField))
- {
- throw new Exception("指定了ItemSourceUrl就一定要设置ValueField");
- }
- }
- if (!string.IsNullOrEmpty(this.TextField))
- {
- dictOptions.Add("textField", "'" + this.TextField + "'");
- }
- if (!string.IsNullOrEmpty(this.ValueField))
- {
- dictOptions.Add("valueField", "'" + this.ValueField + "'");
- }
- //if (!string.IsNullOrEmpty(this.OnSelect))
- //{
- // dictOptions.Add("onSelect", this.OnSelect);
- //}
- dictOptions.Add("onSelect",
- "function(record){ BowinFunction.Combobox.OnSelect.call(this,record," +
- (string.IsNullOrEmpty(this.OnSelect) ?
- "null" :
- this.OnSelect.IndexOf("(") > 0 ?
- this.OnSelect.Substring(0, this.OnSelect.IndexOf("(")) :
- this.OnSelect
- ) + "); }");
- dictOptions.Add("onUnselect", "function(record){ BowinFunction.Combobox.OnUnselect.call(this,record," +
- (string.IsNullOrEmpty(this.OnUnselect) ?
- "null" :
- this.OnUnselect.IndexOf("(") > 0 ?
- this.OnUnselect.Substring(0, this.OnUnselect.IndexOf("(")) :
- this.OnUnselect
- ) + "); }");
- dictOptions.Add("onChange", "function(newValue,oldValue){ BowinFunction.Combobox.OnChange.call(this,newValue,oldValue," +
- (string.IsNullOrEmpty(this.OnChange) ?
- "null" :
- this.OnChange.IndexOf("(") > 0 ?
- this.OnChange.Substring(0, this.OnChange.IndexOf("(")) :
- this.OnChange
- ) + "); }");
- dictOptions.Add("onLoadSuccess", "function(){ BowinFunction.Combobox.OnLoadSuccess.call(this," +
- (string.IsNullOrEmpty(this.OnLoadSuccess) ?
- "null" :
- this.OnLoadSuccess.IndexOf("(") > 0 ?
- this.OnLoadSuccess.Substring(0, this.OnLoadSuccess.IndexOf("(")) :
- this.OnLoadSuccess
- ) + "); }");
- if (!string.IsNullOrEmpty(this.OnBeforeLoad))
- {
- dictOptions.Add("onBeforeLoad", this.OnBeforeLoad);
- }
- if (!string.IsNullOrEmpty(this.Formatter))
- {
- dictOptions.Add("formatter", this.Formatter);
- }
- if (this.Width.HasValue)
- {
- dictOptions.Add("width", this.Width.ToString());
- dictOptions.Add("panelWidth", this.Width.ToString());
- }
- //else
- //{
- // dictOptions.Add("panelWidth", "'auto'");
- //}
- if (this.Height.HasValue)
- {
- dictOptions.Add("panelHeight", this.Height.ToString());
- }
- else
- {
- dictOptions.Add("panelHeight", "'auto'");
- }
- if (string.IsNullOrEmpty(this.ItemSourceUrl))
- {
- if (this.BindType == DropdownListBindType.PleaseSelect)
- {
- ItemList.Insert(0, new DropdownListItem() { Text = "请选择", Value = PLEASE_SELECT });
- }
- else if (this.BindType == DropdownListBindType.SelectAll)
- {
- ItemList.Insert(0, new DropdownListItem() { Text = "全部", Value = SELECT_ALL });
- }
- }
- if (this.SelectedValue != null)
- {
- if (this.SelectedValue.GetType().BaseType != typeof(Array))
- dictOptions.Add("value", "'" + this.SelectedValue + "'");
- else
- {
- var valueArray = new List<string>();
- foreach (var item in ((Array)this.SelectedValue))
- {
- valueArray.Add(item.ToString());
- }
- dictOptions.Add("value", string.Concat("[", string.Join(",", valueArray.Select(x => string.Format("'{0}'", x.ToString()))), "]"));
- }
- }
- if (ItemList.Count > 0)
- {
- dictOptions.Add("data",
- string.Concat("[",
- string.Join(",", ItemList.Select(x => string.Format("{{ Text:'{0}',Value:'{1}' }}", x.Text, x.Value))),
- "]"));
- }
- if (IsEditable.HasValue)
- {
- dictOptions.Add("editable", this.IsEditable.Value.ToString().ToLower());
- }
- if (IsMultiple.HasValue)
- {
- dictOptions.Add("multiple", this.IsMultiple.Value.ToString().ToLower());
- }
- if (this.IsEnabled.HasValue)
- {
- dictOptions.Add("disabled", (!this.IsEnabled.Value).ToString().ToLower());
- }
- //dictOptions.Add("required", "true");
- dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
- #endregion data-options
- //if (!string.IsNullOrEmpty(Reload))
- // dictPropertyList.Add("onchange", string.Format("JavaScript:BowinFunction.DropdownListReload(this,'{0}','{1}');", Reload, string.IsNullOrEmpty(ReloadSource) ? "" : ReloadSource));
- return dictPropertyList;
- }
- public override string Render()
- {
- TagBuilder tabBuilder = new TagBuilder("input");
- tabBuilder.MergeAttributes<string, string>(GetPropertyList());
- if (this.Validator != null)
- {
- tabBuilder.MergeAttributes(this.Validator.GetOptions());
- }
- if (this.Attributes != null)
- {
- tabBuilder.MergeAttributes<string, string>(this.Attributes);
- }
- if (this.IsRequired.HasValue && this.IsRequired.Value)
- {
- tabBuilder.AddCssClass(this.Validator.ValidatorClass);
- tabBuilder.MergeAttributes(this.Validator.GetOptions());
- }
- return tabBuilder.ToString(TagRenderMode.SelfClosing);
- //StringBuilder htmlBuilder = new StringBuilder();
- //htmlBuilder.Append("<input ");
- //htmlBuilder.Append(string.Join(" ", tabBuilder.Attributes.Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value))));
- //htmlBuilder.AppendLine(" />");
- //return htmlBuilder.ToString();
- }
- public static DropdownList CreateControl(DropdownListOptions dropdownListOptions, IDictionary<string, string> attributes = null)
- {
- DropdownList dropdownList = new DropdownList();
- dropdownList.BindType = dropdownListOptions.BindType;
- dropdownList.UrlParameters = dropdownListOptions.UrlParameters;
- dropdownList.CssClass = dropdownListOptions.CssClass;
- dropdownList.Formatter = dropdownListOptions.Formatter;
- dropdownList.Height = dropdownListOptions.Height;
- dropdownList.ID = dropdownListOptions.ID;
- if (dropdownListOptions.ItemList != null)
- {
- dropdownList.ItemList.AddRange(dropdownListOptions.ItemList);
- }
- dropdownList.ItemSourceUrl = dropdownListOptions.ItemSourceUrl;
- dropdownList.Name = dropdownListOptions.Name;
- dropdownList.OnSelect = dropdownListOptions.OnSelect;
- dropdownList.OnUnselect = dropdownListOptions.OnUnselect;
- dropdownList.OnChange = dropdownListOptions.OnChange;
- dropdownList.OnBeforeLoad = dropdownListOptions.OnBeforeLoad;
- dropdownList.SelectedValue = dropdownListOptions.SelectedValue;
- dropdownList.TextField = string.IsNullOrWhiteSpace(dropdownListOptions.TextField) ? "Text" : dropdownListOptions.TextField;
- dropdownList.ValueField = string.IsNullOrWhiteSpace(dropdownListOptions.ValueField) ? "Value" : dropdownListOptions.ValueField;
- dropdownList.Width = dropdownListOptions.Width;
- dropdownList.IsEditable = dropdownListOptions.IsEditable;
- dropdownList.IsMultiple = dropdownListOptions.IsMultiple;
- dropdownList.Attributes = attributes;
- dropdownList.Validator = dropdownListOptions.Validator;
- dropdownList.IsRequired = dropdownListOptions.IsRequired;
- dropdownList.IsEnabled = dropdownListOptions.IsEnabled;
- dropdownList.IsShow = dropdownListOptions.IsShow;
- dropdownList.OnLoadSuccess = dropdownListOptions.OnLoadSuccess;
- //dropdownList.Reload = dropdownListOptions.Reload;
- //dropdownList.ReloadSource = dropdownListOptions.ReloadSource;
- return dropdownList;
- }
- public static void FormatDropdownItemList(DropdownListBindType bindType, IList<DropdownListItem> items)
- {
- if (bindType == DropdownListBindType.PleaseSelect)
- {
- items.Insert(0, DropdownList.PleaseSelectItem);
- }
- else if (bindType == DropdownListBindType.SelectAll)
- {
- items.Insert(0, DropdownList.SelectAllItem);
- }
- }
- private static string ObjectToString(Array o)
- {
- return o.ToString();
- }
- private static string IntToString(int o)
- {
- return o.ToString();
- }
- }
- [Serializable]
- public class DropdownListItem
- {
- public DropdownListItem()
- {
- //IsSelected = false;
- }
- public string Text { get; set; }
- public object Value { get; set; }
- //public bool IsSelected { get; set; }
- }
- public enum DropdownListBindType
- {
- PleaseSelect,
- SelectAll,
- None
- }
- }
|