102 lines
4.4 KiB
C#
102 lines
4.4 KiB
C#
using System.Data;
|
|
using System.Text;
|
|
namespace WX.CRM.WebHelper
|
|
{
|
|
public class DataTableHandler
|
|
{
|
|
/// <summary>
|
|
/// 获取有分页的html表格(“HIDDEN_字段名”这种格式的字段不显示)
|
|
/// </summary>
|
|
/// <param name="tab">DataTable数据</param>
|
|
/// <param name="PageSize">一页显示的数据</param>
|
|
/// <returns></returns>
|
|
public static string GetPageTableHtml(DataTable tab, int pageSize, int pageIndex, string css = "bordered")
|
|
{
|
|
StringBuilder html = new StringBuilder();
|
|
|
|
if (tab == null)
|
|
return html.ToString();
|
|
html.AppendLine("<table class='" + css + "' style='width:100%;'>");
|
|
//===============================================#头部#======================================================\\
|
|
html.AppendLine(" <thead>");
|
|
html.AppendLine(" <tr>");
|
|
html.AppendLine(" <th> </th>");
|
|
foreach (DataColumn column in tab.Columns)
|
|
{
|
|
if (column.ColumnName.IndexOf("HIDDEN_") == -1)
|
|
html.AppendLine(string.Format(" <th field='{0}'>{1}</th>", column.ColumnName, column.Caption));
|
|
}
|
|
|
|
html.AppendLine(" </tr>");
|
|
html.AppendLine("</thead>");
|
|
//===============================================#####=========================================================\\
|
|
if (tab.Rows.Count == 0)
|
|
return html.ToString();
|
|
//===============================================#数据#======================================================\\
|
|
html.AppendLine(" <tbody>");
|
|
int startIndex = 0;
|
|
int endIndex = pageSize;
|
|
if (pageIndex > 1)
|
|
{
|
|
startIndex = pageSize * (pageIndex - 1);
|
|
endIndex = pageSize * pageIndex;
|
|
}
|
|
if (endIndex > tab.Rows.Count)
|
|
endIndex = tab.Rows.Count;
|
|
for (int i = startIndex; i < endIndex; i++)
|
|
{
|
|
html.AppendLine(" <tr>");
|
|
html.AppendLine(string.Format(" <td>{0}</td>", i + 1));
|
|
for (int x = 0; x < tab.Columns.Count; x++)
|
|
{
|
|
if (tab.Columns[x].ColumnName.IndexOf("HIDDEN_") == -1)
|
|
html.AppendLine(string.Format(" <td>{0}</td>", tab.Rows[i][x]));
|
|
}
|
|
//foreach (var obj in tab.Rows[i].ItemArray)
|
|
//{
|
|
// html.AppendLine(string.Format(" <td>{0}</td>", obj));
|
|
//}
|
|
html.AppendLine(" </tr>");
|
|
}
|
|
html.AppendLine(" </tbody>");
|
|
//===============================================#####======================================================\\
|
|
//===============================================尾部=======================================================\\
|
|
html.AppendLine(" <tfoot>");
|
|
html.AppendLine(" <tr class='tr_pagenumber'>");
|
|
html.AppendLine("<td colspan='" + (tab.Columns.Count + 1) + "'>" + PageHelper.GetPage(tab.Rows.Count, pageIndex, pageSize, "LoadData") + "</td>");
|
|
html.AppendLine(" </tr>");
|
|
html.AppendLine(" </tfoot>");
|
|
|
|
html.AppendLine("</table>");
|
|
|
|
return html.ToString();
|
|
|
|
}
|
|
//public static string GetPageTableHtml(DataTable tab, int PageSize, int PageIndex,string pagination)
|
|
//{
|
|
// string str = string.Empty;
|
|
// Pager pager = new Pager() { page = PageIndex, rows = PageSize };
|
|
// string tableId = "tablist";
|
|
// Table table = new Table(tableId);
|
|
// table.gridPager = pager;
|
|
// //添加表头
|
|
// foreach (DataColumn column in tab.Columns)
|
|
// {
|
|
// table.AddHeadCol(column.ColumnName, "", column.Caption);
|
|
// }
|
|
// table.AddHeadRow();
|
|
// //添加数据行
|
|
// for (int i = 0; i < tab.Rows.Count; i++)
|
|
// {
|
|
// foreach (var obj in tab.Rows[i].ItemArray)
|
|
// {
|
|
// table.AddCol(obj);
|
|
// }
|
|
// table.AddRow();
|
|
// }
|
|
// return table.GetTable() + Pagination.GetPage(pager, tableId, pagination);
|
|
//}
|
|
|
|
}
|
|
}
|