DraggableList.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. using System.Drawing;
  7. using Bowin.Common.Utility;
  8. namespace Bowin.Web.Controls.Mvc
  9. {
  10. public class DraggableList : BaseFormControl
  11. {
  12. public List<DraggableItem> ItemList { get; set; }
  13. public string ItemSourceUrl { get; set; }
  14. public int ItemHeight { get; set; }
  15. public string TitleField { get; set; }
  16. public string IconField { get; set; }
  17. public string ValueField { get; set; }
  18. public int? Width { get; set; }
  19. public int? Height { get; set; }
  20. public Color ForeColor { get; set; }
  21. public DraggableList()
  22. {
  23. ItemList = new List<DraggableItem>();
  24. }
  25. protected IDictionary<string, string> GetPropertyList()
  26. {
  27. var dictPropertyList = new Dictionary<string, string>();
  28. if (!string.IsNullOrEmpty(this.ID))
  29. {
  30. dictPropertyList.Add("id", this.ID);
  31. }
  32. if (!string.IsNullOrEmpty(this.Name))
  33. {
  34. dictPropertyList.Add("name", this.Name);
  35. }
  36. dictPropertyList.Add("class", "easyui-draggableList");
  37. string style = "";
  38. if (this.Width.HasValue)
  39. {
  40. style += "width: " + this.Width.Value.ToString() + "px;";
  41. }
  42. else
  43. {
  44. style += "width: 100%;";
  45. }
  46. if (this.Height.HasValue)
  47. {
  48. style += "height: " + this.Height.Value.ToString() + "px;";
  49. }
  50. else
  51. {
  52. style += "height: 100%;";
  53. }
  54. style += "overflow-x: scroll;";
  55. dictPropertyList.Add("style", style);
  56. #region data-options
  57. var dictOptions = new Dictionary<string, string>();
  58. if (this.ForeColor != null)
  59. {
  60. dictOptions.Add("foreColor", "'#" + this.ForeColor.ToRGBString() + "'");
  61. }
  62. if (!string.IsNullOrEmpty(this.ItemSourceUrl))
  63. {
  64. dictOptions.Add("url", "'" + this.ItemSourceUrl + "'");
  65. if (string.IsNullOrEmpty(this.TitleField))
  66. {
  67. throw new Exception("指定了ItemSourceUrl就一定要设置TitleField");
  68. }
  69. }
  70. if (!string.IsNullOrEmpty(this.TitleField))
  71. {
  72. dictOptions.Add("titleField", "'" + this.TitleField + "'");
  73. }
  74. if (!string.IsNullOrEmpty(this.ValueField))
  75. {
  76. dictOptions.Add("valueField", "'" + this.ValueField + "'");
  77. }
  78. if (!string.IsNullOrEmpty(this.IconField))
  79. {
  80. dictOptions.Add("iconField", "'" + this.IconField + "'");
  81. }
  82. dictOptions.Add("itemHeight", this.ItemHeight.ToString());
  83. dictPropertyList.Add("data-options", string.Join(",", dictOptions.Select(x => string.Format("{0}:{1}", x.Key, x.Value))));
  84. #endregion data-options
  85. return dictPropertyList;
  86. }
  87. public override string Render()
  88. {
  89. TagBuilder tabBuilder = new TagBuilder("div");
  90. tabBuilder.MergeAttributes<string, string>(GetPropertyList());
  91. if (this.Attributes != null)
  92. {
  93. tabBuilder.MergeAttributes<string, string>(this.Attributes);
  94. }
  95. return tabBuilder.ToString(TagRenderMode.Normal);
  96. }
  97. public static DraggableList CreateControl(DraggableListControlOptions dragableListOptions, IDictionary<string, string> attributes = null)
  98. {
  99. DraggableList dragableList = new DraggableList();
  100. dragableList.ID = dragableListOptions.ID;
  101. if (dragableListOptions.ItemList != null)
  102. {
  103. dragableList.ItemList.AddRange(dragableListOptions.ItemList);
  104. }
  105. dragableList.Name = dragableListOptions.Name;
  106. dragableList.Attributes = attributes;
  107. dragableList.ItemSourceUrl = dragableListOptions.ItemSourceUrl;
  108. dragableList.ItemList = dragableListOptions.ItemList;
  109. dragableList.ItemHeight = dragableListOptions.ItemHeight;
  110. dragableList.TitleField = dragableListOptions.TitleField;
  111. dragableList.ValueField = dragableListOptions.ValueField;
  112. dragableList.IconField = dragableListOptions.IconField;
  113. dragableList.Width = dragableListOptions.Width;
  114. dragableList.Height = dragableListOptions.Height;
  115. dragableList.ForeColor = dragableListOptions.ForeColor;
  116. return dragableList;
  117. }
  118. }
  119. }