123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Web.Mvc;
- namespace Bowin.Web.Controls.Mvc
- {
- public class AutoComplete : BaseControl
- {
- public int? Width { get; set; }
- public int? ScrollHeight { get; set; }
- public bool? MatchContains { get; set; }
- public bool? MustMatch { get; set; }
- public bool? AutoFill { get; set; }
- public string CustomFormatterFun { get; set; }
- public string Url { get; set; }
- public string TextField { get; set; }
- public string ValueField { get; set; }
- public object DefaultValue { get; set; }
- protected IDictionary<string, string> GetPropertyList()
- {
- var dictPropertyList = new Dictionary<string, string>();
- string id;
- if (!string.IsNullOrEmpty(this.ID))
- {
- id = this.ID;
- }
- else
- {
- id = Guid.NewGuid().ToString();
- }
- dictPropertyList.Add("id", id);
- if (!string.IsNullOrEmpty(this.Name))
- {
- dictPropertyList.Add("name", this.Name + "_text");
- }
- dictPropertyList.Add("class", string.Format("easyui-autoComplete {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
- #region data-options
- var dictOptions = new Dictionary<string, string>();
- if (Width.HasValue)
- dictOptions.Add("width", Width.ToString());
- if (ScrollHeight.HasValue)
- dictOptions.Add("scrollHeight", ScrollHeight.ToString());
- if (MatchContains.HasValue)
- dictOptions.Add("matchContains", MatchContains.ToString().ToLower());
- if (MustMatch.HasValue)
- dictOptions.Add("mustMatch", MustMatch.ToString().ToLower());
- if (AutoFill.HasValue)
- dictOptions.Add("autoFill", AutoFill.ToString().ToLower());
- if (!string.IsNullOrEmpty(Url))
- dictOptions.Add("url", "\'" + Url + "\'");
- else
- throw new Exception("必须指定Url属性。");
- if (!string.IsNullOrEmpty(TextField))
- dictOptions.Add("textField", "\'" + TextField + "\'");
- else
- throw new Exception("必须指定TextField属性。");
- if (!string.IsNullOrEmpty(ValueField))
- dictOptions.Add("valueField", "\'" + ValueField + "\'");
- else
- throw new Exception("必须指定ValueField属性。");
- if (!string.IsNullOrEmpty(CustomFormatterFun))
- dictOptions.Add("formatter", CustomFormatterFun);
- else
- dictOptions.Add("formatter", "function (row, i, max) { return row." + TextField + "; }");
- dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
- #endregion data-options
- return dictPropertyList;
- }
- public static BaseControl CreateControl(AutoCompleteOptions autoCompleteOptions, IDictionary<string, string> attributes = null)
- {
- AutoComplete autoComplete = new AutoComplete();
- autoComplete.Width = autoCompleteOptions.Width;
- autoComplete.ScrollHeight = autoCompleteOptions.ScrollHeight;
- autoComplete.MatchContains = autoCompleteOptions.MatchContains;
- autoComplete.MustMatch = autoCompleteOptions.MustMatch;
- autoComplete.AutoFill = autoCompleteOptions.AutoFill;
- autoComplete.CustomFormatterFun = autoCompleteOptions.CustomFormatterFun;
- autoComplete.TextField = autoCompleteOptions.TextField;
- autoComplete.ValueField = autoCompleteOptions.ValueField;
- autoComplete.DefaultValue = autoCompleteOptions.DefaultValue;
- autoComplete.ID = autoCompleteOptions.ID ?? "txt_" + Guid.NewGuid().ToString().Substring(0, 6);
- autoComplete.Name = autoCompleteOptions.Name;
- autoComplete.Title = autoCompleteOptions.Title;
- autoComplete.Url = autoCompleteOptions.Url;
- autoComplete.IsEnabled = autoCompleteOptions.IsEnabled;
- autoComplete.IsShow = autoCompleteOptions.IsShow;
- autoComplete.CssClass = autoCompleteOptions.CssClass;
- if (attributes != null)
- {
- autoComplete.Attributes = attributes;
- }
- return autoComplete;
- }
- public override string Render()
- {
- TagBuilder tagBuilder = new TagBuilder("input");
- TagBuilder hiddenTagBuilder = new TagBuilder("input");
- tagBuilder.MergeAttributes(GetPropertyList());
- hiddenTagBuilder.Attributes.Add("type", "hidden");
- hiddenTagBuilder.Attributes.Add("id", this.ID + "_value");
- hiddenTagBuilder.Attributes.Add("name", this.Name);
- hiddenTagBuilder.Attributes.Add("value", (this.DefaultValue ?? "").ToString());
- return tagBuilder.ToString(TagRenderMode.Normal)
- + hiddenTagBuilder.ToString(TagRenderMode.Normal);
- }
- }
- }
|