ScrollerTable.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web;
  7. /// <summary>
  8. /// Summary description for ScrollerTable
  9. /// </summary>
  10. public class ScrollerTable
  11. {
  12. public ScrollerTable()
  13. {
  14. }
  15. /// <summary>
  16. /// 构造固定表头和固定列的table
  17. /// </summary>
  18. /// <param name="obj">数据表</param>
  19. /// <param name="staticColCount">固定列数(需要固定的列要放在左边)</param>
  20. /// <param name="widthPerCol">每列宽度</param>
  21. /// <returns></returns>
  22. public string GetScrollerTable(DataTable obj, int staticColCount, List<string> widthPerCol, int tableWidth, int tableHeight)
  23. {
  24. //活动表宽
  25. string scrollerTableWidth = GetScrollerTbaleWidth(staticColCount, widthPerCol);
  26. //活动div占屏幕宽度
  27. string scrollerDivWidth = GetScrollerDivWidth(staticColCount, widthPerCol, tableWidth);
  28. StringBuilder table = new StringBuilder();
  29. table.Append("<div id='ScrollerTable'>");
  30. #region 构造表头
  31. table.Append("<div id='thead' style='display: flex;'>");
  32. #region 构造固定列
  33. if (staticColCount > 0)
  34. {
  35. table.Append("<div id='headLeft' style='display: inline-flex;'> <table cellpadding='0' cellspacing='0'> <tr>");
  36. for (int i = 0; i < staticColCount; i++)
  37. {
  38. table.Append("<td style='width:" + widthPerCol[i] + "px;'>" + obj.Columns[i].ColumnName + "</td>");
  39. }
  40. table.Append("</tr></table></div>");
  41. }
  42. #endregion
  43. #region 构造活动列
  44. table.Append(" <div id='headRight' style='overflow: hidden; display: inline-flex;width:" + scrollerDivWidth + "px;'>");
  45. table.Append(" <table cellpadding='0' cellspacing='0' style='width: " + scrollerTableWidth + "px; '><tr>");
  46. for (int i = staticColCount; i < obj.Columns.Count; i++)
  47. {
  48. table.Append("<td style='width:" + widthPerCol[i] + "px;'>" + obj.Columns[i].ColumnName + "</td>");
  49. }
  50. table.Append(" </tr></table ></div> ");
  51. #endregion
  52. table.Append("</div>");
  53. #endregion
  54. #region 构造表主体
  55. table.Append("<div id='tbody'>");
  56. #region 构造固定列内容
  57. if (staticColCount > 0)
  58. {
  59. table.Append("<div id='bodyLeft' style='display: inline-flex;overflow: hidden;height:" + tableHeight + "px;'> <table cellpadding='0' cellspacing='0'>");
  60. foreach (DataRow row in obj.Rows)
  61. {
  62. table.Append("<tr>");
  63. for (int i = 0; i < staticColCount; i++)
  64. {
  65. table.Append("<td style='width:" + widthPerCol[i] + "px;'>" + row[i] + "</td>");
  66. }
  67. table.Append("</tr>");
  68. }
  69. table.Append("</table></div>");
  70. }
  71. #endregion
  72. #region 构造活动列内容
  73. table.Append("<div id='bodyRight' style='display: inline-flex;overflow: auto;height:" + tableHeight + "px;width:"+ scrollerDivWidth + "px; '>");
  74. table.Append(" <table cellpadding='0' cellspacing='0' style='width: " + scrollerTableWidth + "px; '>");
  75. foreach (DataRow row in obj.Rows)
  76. {
  77. table.Append("<tr>");
  78. for (int i = staticColCount; i < obj.Columns.Count; i++)
  79. {
  80. table.Append("<td style='width:"+ widthPerCol[i] + "px;'>"+ row[i] + "</td>");
  81. }
  82. table.Append("</tr>");
  83. }
  84. table.Append("</table></div>");
  85. #endregion
  86. table.Append("</div>");
  87. #endregion
  88. table.Append("</div>");
  89. return table.ToString();
  90. }
  91. /// <summary>
  92. /// 获取活动表宽
  93. /// </summary>
  94. /// <param name="staticColCount"></param>
  95. /// <param name="widthPerCol"></param>
  96. /// <returns></returns>
  97. private string GetScrollerTbaleWidth(int staticColCount, List<string> widthPerCol)
  98. {
  99. int width = 0;
  100. for (int i = staticColCount; i < widthPerCol.Count; i++)
  101. {
  102. width += Convert.ToInt32(widthPerCol[i]);
  103. }
  104. return width.ToString();
  105. }
  106. private string GetScrollerDivWidth(int staticColCount, List<string> widthPerCol, int tableWidth)
  107. {
  108. int width = 0;
  109. for (int i = 0; i < staticColCount; i++)
  110. {
  111. width += Convert.ToInt32(widthPerCol[i]);
  112. }
  113. return (tableWidth - width).ToString();
  114. }
  115. }