AutoComplete.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. namespace Bowin.Web.Controls.Mvc
  7. {
  8. public class AutoComplete : BaseControl
  9. {
  10. public int? Width { get; set; }
  11. public int? ScrollHeight { get; set; }
  12. public bool? MatchContains { get; set; }
  13. public bool? MustMatch { get; set; }
  14. public bool? AutoFill { get; set; }
  15. public string CustomFormatterFun { get; set; }
  16. public string Url { get; set; }
  17. public string TextField { get; set; }
  18. public string ValueField { get; set; }
  19. public object DefaultValue { get; set; }
  20. protected IDictionary<string, string> GetPropertyList()
  21. {
  22. var dictPropertyList = new Dictionary<string, string>();
  23. string id;
  24. if (!string.IsNullOrEmpty(this.ID))
  25. {
  26. id = this.ID;
  27. }
  28. else
  29. {
  30. id = Guid.NewGuid().ToString();
  31. }
  32. dictPropertyList.Add("id", id);
  33. if (!string.IsNullOrEmpty(this.Name))
  34. {
  35. dictPropertyList.Add("name", this.Name + "_text");
  36. }
  37. dictPropertyList.Add("class", string.Format("easyui-autoComplete {0}", string.IsNullOrEmpty(this.CssClass) ? "" : this.CssClass));
  38. #region data-options
  39. var dictOptions = new Dictionary<string, string>();
  40. if (Width.HasValue)
  41. dictOptions.Add("width", Width.ToString());
  42. if (ScrollHeight.HasValue)
  43. dictOptions.Add("scrollHeight", ScrollHeight.ToString());
  44. if (MatchContains.HasValue)
  45. dictOptions.Add("matchContains", MatchContains.ToString().ToLower());
  46. if (MustMatch.HasValue)
  47. dictOptions.Add("mustMatch", MustMatch.ToString().ToLower());
  48. if (AutoFill.HasValue)
  49. dictOptions.Add("autoFill", AutoFill.ToString().ToLower());
  50. if (!string.IsNullOrEmpty(Url))
  51. dictOptions.Add("url", "\'" + Url + "\'");
  52. else
  53. throw new Exception("必须指定Url属性。");
  54. if (!string.IsNullOrEmpty(TextField))
  55. dictOptions.Add("textField", "\'" + TextField + "\'");
  56. else
  57. throw new Exception("必须指定TextField属性。");
  58. if (!string.IsNullOrEmpty(ValueField))
  59. dictOptions.Add("valueField", "\'" + ValueField + "\'");
  60. else
  61. throw new Exception("必须指定ValueField属性。");
  62. if (!string.IsNullOrEmpty(CustomFormatterFun))
  63. dictOptions.Add("formatter", CustomFormatterFun);
  64. else
  65. dictOptions.Add("formatter", "function (row, i, max) { return row." + TextField + "; }");
  66. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  67. #endregion data-options
  68. return dictPropertyList;
  69. }
  70. public static BaseControl CreateControl(AutoCompleteOptions autoCompleteOptions, IDictionary<string, string> attributes = null)
  71. {
  72. AutoComplete autoComplete = new AutoComplete();
  73. autoComplete.Width = autoCompleteOptions.Width;
  74. autoComplete.ScrollHeight = autoCompleteOptions.ScrollHeight;
  75. autoComplete.MatchContains = autoCompleteOptions.MatchContains;
  76. autoComplete.MustMatch = autoCompleteOptions.MustMatch;
  77. autoComplete.AutoFill = autoCompleteOptions.AutoFill;
  78. autoComplete.CustomFormatterFun = autoCompleteOptions.CustomFormatterFun;
  79. autoComplete.TextField = autoCompleteOptions.TextField;
  80. autoComplete.ValueField = autoCompleteOptions.ValueField;
  81. autoComplete.DefaultValue = autoCompleteOptions.DefaultValue;
  82. autoComplete.ID = autoCompleteOptions.ID ?? "txt_" + Guid.NewGuid().ToString().Substring(0, 6);
  83. autoComplete.Name = autoCompleteOptions.Name;
  84. autoComplete.Title = autoCompleteOptions.Title;
  85. autoComplete.Url = autoCompleteOptions.Url;
  86. autoComplete.IsEnabled = autoCompleteOptions.IsEnabled;
  87. autoComplete.IsShow = autoCompleteOptions.IsShow;
  88. autoComplete.CssClass = autoCompleteOptions.CssClass;
  89. if (attributes != null)
  90. {
  91. autoComplete.Attributes = attributes;
  92. }
  93. return autoComplete;
  94. }
  95. public override string Render()
  96. {
  97. TagBuilder tagBuilder = new TagBuilder("input");
  98. TagBuilder hiddenTagBuilder = new TagBuilder("input");
  99. tagBuilder.MergeAttributes(GetPropertyList());
  100. hiddenTagBuilder.Attributes.Add("type", "hidden");
  101. hiddenTagBuilder.Attributes.Add("id", this.ID + "_value");
  102. hiddenTagBuilder.Attributes.Add("name", this.Name);
  103. hiddenTagBuilder.Attributes.Add("value", (this.DefaultValue ?? "").ToString());
  104. return tagBuilder.ToString(TagRenderMode.Normal)
  105. + hiddenTagBuilder.ToString(TagRenderMode.Normal);
  106. }
  107. }
  108. }