123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Web;
- /// <summary>
- /// Summary description for ScrollerTable
- /// </summary>
- public class ScrollerTable
- {
- public ScrollerTable()
- {
- }
- /// <summary>
- /// 构造固定表头和固定列的table
- /// </summary>
- /// <param name="obj">数据表</param>
- /// <param name="staticColCount">固定列数(需要固定的列要放在左边)</param>
- /// <param name="widthPerCol">每列宽度</param>
- /// <returns></returns>
- public string GetScrollerTable(DataTable obj, int staticColCount, List<string> widthPerCol, int tableWidth, int tableHeight)
- {
- //活动表宽
- string scrollerTableWidth = GetScrollerTbaleWidth(staticColCount, widthPerCol);
- //活动div占屏幕宽度
- string scrollerDivWidth = GetScrollerDivWidth(staticColCount, widthPerCol, tableWidth);
- StringBuilder table = new StringBuilder();
- table.Append("<div id='ScrollerTable'>");
- #region 构造表头
- table.Append("<div id='thead' style='display: flex;'>");
- #region 构造固定列
- if (staticColCount > 0)
- {
- table.Append("<div id='headLeft' style='display: inline-flex;'> <table cellpadding='0' cellspacing='0'> <tr>");
- for (int i = 0; i < staticColCount; i++)
- {
- table.Append("<td style='width:" + widthPerCol[i] + "px;'>" + obj.Columns[i].ColumnName + "</td>");
- }
- table.Append("</tr></table></div>");
- }
- #endregion
- #region 构造活动列
- table.Append(" <div id='headRight' style='overflow: hidden; display: inline-flex;width:" + scrollerDivWidth + "px;'>");
- table.Append(" <table cellpadding='0' cellspacing='0' style='width: " + scrollerTableWidth + "px; '><tr>");
- for (int i = staticColCount; i < obj.Columns.Count; i++)
- {
- table.Append("<td style='width:" + widthPerCol[i] + "px;'>" + obj.Columns[i].ColumnName + "</td>");
- }
- table.Append(" </tr></table ></div> ");
- #endregion
- table.Append("</div>");
- #endregion
- #region 构造表主体
- table.Append("<div id='tbody'>");
- #region 构造固定列内容
- if (staticColCount > 0)
- {
- table.Append("<div id='bodyLeft' style='display: inline-flex;overflow: hidden;height:" + tableHeight + "px;'> <table cellpadding='0' cellspacing='0'>");
- foreach (DataRow row in obj.Rows)
- {
- table.Append("<tr>");
- for (int i = 0; i < staticColCount; i++)
- {
- table.Append("<td style='width:" + widthPerCol[i] + "px;'>" + row[i] + "</td>");
- }
- table.Append("</tr>");
- }
- table.Append("</table></div>");
- }
- #endregion
- #region 构造活动列内容
- table.Append("<div id='bodyRight' style='display: inline-flex;overflow: auto;height:" + tableHeight + "px;width:"+ scrollerDivWidth + "px; '>");
- table.Append(" <table cellpadding='0' cellspacing='0' style='width: " + scrollerTableWidth + "px; '>");
- foreach (DataRow row in obj.Rows)
- {
- table.Append("<tr>");
- for (int i = staticColCount; i < obj.Columns.Count; i++)
- {
- table.Append("<td style='width:"+ widthPerCol[i] + "px;'>"+ row[i] + "</td>");
- }
- table.Append("</tr>");
- }
- table.Append("</table></div>");
- #endregion
- table.Append("</div>");
- #endregion
- table.Append("</div>");
- return table.ToString();
- }
- /// <summary>
- /// 获取活动表宽
- /// </summary>
- /// <param name="staticColCount"></param>
- /// <param name="widthPerCol"></param>
- /// <returns></returns>
- private string GetScrollerTbaleWidth(int staticColCount, List<string> widthPerCol)
- {
- int width = 0;
- for (int i = staticColCount; i < widthPerCol.Count; i++)
- {
- width += Convert.ToInt32(widthPerCol[i]);
- }
- return width.ToString();
- }
- private string GetScrollerDivWidth(int staticColCount, List<string> widthPerCol, int tableWidth)
- {
- int width = 0;
- for (int i = 0; i < staticColCount; i++)
- {
- width += Convert.ToInt32(widthPerCol[i]);
- }
- return (tableWidth - width).ToString();
- }
- }
|