490 lines
20 KiB
C#
490 lines
20 KiB
C#
using System.Collections;
|
||
using System.Text;
|
||
using WX.CRM.Common;
|
||
|
||
namespace WX.CRM.WebHelper
|
||
{
|
||
public class Table
|
||
{
|
||
|
||
|
||
private StringBuilder table = new StringBuilder();
|
||
private StringBuilder temp = new StringBuilder();
|
||
private StringBuilder foot = new StringBuilder();
|
||
private ArrayList fields = new ArrayList();
|
||
private string columns = string.Empty;
|
||
private int colnum = 0;
|
||
private int rowCout = 0;//行索引
|
||
private int fieldIndex = 0;
|
||
/// <summary>
|
||
/// 默认为居左padding-left:5px
|
||
/// </summary>
|
||
public string tdLeft5 = "text-align:left;padding-left:5px;";
|
||
/// <summary>
|
||
/// 默认为居左padding:5px;
|
||
/// </summary>
|
||
public string tdLeftPadding5 = "text-align:left;padding:5px;";
|
||
/// <summary>
|
||
/// 是否显示复选框(默认为false)
|
||
/// </summary>
|
||
public bool isCheckbox { get; set; }
|
||
/// <summary>
|
||
/// 是否显示行号(默认为true)
|
||
/// </summary>
|
||
public bool isNumber { get; set; }
|
||
/// <summary>
|
||
/// 漂浮表头(默认为false)
|
||
/// </summary>
|
||
public bool isFloatHead { get; set; }
|
||
/// <summary>
|
||
/// 分页信息
|
||
/// </summary>
|
||
public Pager gridPager { get; set; }
|
||
#region 表格操作
|
||
/// <summary>
|
||
/// 创建有ID的table
|
||
/// </summary>
|
||
/// <param name="TableId">表的ID</param>
|
||
public Table(string TableId)
|
||
{
|
||
this.isCheckbox = false;
|
||
this.isNumber = true;
|
||
//if(isThead)
|
||
//{
|
||
table.AppendLine(string.Format("<div class='grid_table' ><table width='100%' id='{0}' state='false' cellspacing='0' cellpadding='0' class='layui-table ' lay-size='sm' columns='[columns]'><thead>", TableId));
|
||
//}
|
||
//else
|
||
//{
|
||
// table.AppendLine(string.Format("<div class='grid_table' ><table width='100%' id='{0}' state='false' cellspacing='0' cellpadding='0' class='layui-table ' lay-size='sm' columns='[columns]'><tbody>", TableId));
|
||
//}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建有ID的table 和表格样式
|
||
/// </summary>
|
||
/// <param name="TableId"></param>
|
||
/// <param name="tableClass"></param>
|
||
public Table(string TableId, string tableClass)
|
||
{
|
||
this.isCheckbox = false;
|
||
this.isNumber = true;
|
||
table.AppendLine(string.Format("<div><table width='100%' id='{0}' state='false' cellspacing='0' cellpadding='0' class='{1}' columns='[columns]'><tbody>", TableId, tableClass));
|
||
}
|
||
/// <summary>
|
||
/// 创建空表(用于只显示数据列)
|
||
/// </summary>
|
||
/// <param name="columns">字段列表(从全段返回)</param>
|
||
/// <param name="isEmpty">是没表头</param>
|
||
|
||
public Table(string columns, bool isNoHead)
|
||
{
|
||
this.isCheckbox = false;
|
||
this.isNumber = true;
|
||
string[] nfields = columns.Split(',');
|
||
foreach (string item in nfields)
|
||
{
|
||
fields.Add(item);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加class头部单元格
|
||
/// </summary>
|
||
/// <param name="field">字段名称</param>
|
||
/// <param name="width">宽度</param>
|
||
/// <param name="title">标题</param>
|
||
/// <param name="classStr">class</param>
|
||
public void AddClassHeadCol(string field, string width, string title, string classStr)
|
||
{
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' width=\"{1}\" class=\"{3}\">{2}</th>\n", field, width, title, classStr);
|
||
colnum++;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加左固定头部单元格
|
||
/// </summary>
|
||
/// <param name="field">字段名称</param>
|
||
/// <param name="title">标题</param>
|
||
/// <param name="width">宽度</param>
|
||
/// <param name="left">离左边的固定距离</param>
|
||
public void AddFixedLeftHeadCol(string field, string title, int width, int left)
|
||
{
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' class='table_fixed' style='left: {2}px;z-index: 999;width:{3}px !important;'>{1}</th>\n", field, title, left, width);
|
||
colnum++;
|
||
}
|
||
|
||
public void AddFixedLeftHeadCol(string field, string title, int width, int left, bool isSort, bool isDefault = false, string sortDefault = "desc")
|
||
{
|
||
if (isSort)
|
||
{
|
||
var sortClass = "";
|
||
if (isDefault)
|
||
{
|
||
if (sortDefault == "desc")
|
||
{
|
||
sortClass = "tablegrid_sort_desc";
|
||
}
|
||
else
|
||
{
|
||
sortClass = "tablegrid_sort_asc";
|
||
}
|
||
}
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' class='table_fixed tablegrid_sort " + sortClass + "' style='left: {2}px;z-index: 999;width:{3}px !important;'>{1}<span class='tablegrid-sort-icon'> </span></th>\n", field, title, left, width);
|
||
colnum++;
|
||
}
|
||
else
|
||
{
|
||
AddFixedLeftHeadCol(field, title, width, left);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加class单元格
|
||
/// </summary>
|
||
/// <param name="value">数据值</param>
|
||
/// <param name="width">宽度</param>
|
||
/// <param name="left">离左边的固定距离</param>
|
||
public void AddFixedLeftCol(object value, int width, int left)
|
||
{
|
||
temp.AppendFormat(" <td field='{1}' class='table_fixed' style='left: {2}px;width:{3}px !important;'>{0}</td>\n", ((value == null || value.ToString().Trim().Equals("")) ? " " : value), (fields.Count <= 1 ? "" : fields[fieldIndex]), left, width);
|
||
fieldIndex++;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加class单元格
|
||
/// </summary>
|
||
/// <param name="value">数据值</param>
|
||
/// <param name="classStr">class</param>
|
||
public void AddClassCol(object value, string classStr)
|
||
{
|
||
temp.AppendFormat(" <td field='{1}' class='{2}'>{0}</td>\n", ((value == null || value.ToString().Trim().Equals("")) ? " " : value), (fields.Count <= 1 ? "" : fields[fieldIndex]), classStr);
|
||
fieldIndex++;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 添加头部单元格
|
||
/// </summary>
|
||
/// <param name="field">字段名称</param>
|
||
/// <param name="width">宽度</param>
|
||
/// <param name="title">标题</param>
|
||
public void AddHeadCol(string field, string width, string title)
|
||
{
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' width=\"{1}\">{2}</th>\n", field, width, title);
|
||
colnum++;
|
||
}
|
||
/// <summary>
|
||
/// 添加Tip头部单元格
|
||
/// </summary>
|
||
/// <param name="field">字段名称</param>
|
||
/// <param name="width">宽度</param>
|
||
/// <param name="title">标题</param>
|
||
public void AddTipHeadCol(string field, string width, string title,string img,string tip)
|
||
{
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' width=\"{1}\">{2}<img style=\"margin-left: 8px;cursor: pointer;\" src=\"{3}\" title=\"{4}\"/></th>\n", field, width, title, img,tip);
|
||
colnum++;
|
||
}
|
||
/// <summary>
|
||
/// 添加头部单元格
|
||
/// </summary>
|
||
/// <param name="field">字段名称</param>
|
||
/// <param name="colspan">合并列</param>
|
||
/// <param name="title">标题</param>
|
||
public void AddHeadCol(string field, int colspan, string title)
|
||
{
|
||
//fields.Add(field);
|
||
//columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' colspan=\"{1}\">{2}</th>\n", field, colspan, title);
|
||
colnum++;
|
||
}
|
||
/// <summary>
|
||
/// 合并行
|
||
/// </summary>
|
||
/// <param name="field"></param>
|
||
/// <param name="rowspan"></param>
|
||
/// <param name="title"></param>
|
||
public void AddHeadColRowSpan(string field, int rowspan, string title)
|
||
{
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' rowspan=\"{1}\">{2}</th>\n", field, rowspan, title);
|
||
colnum++;
|
||
}
|
||
/// <summary>
|
||
/// 添加头部单元格
|
||
/// </summary>
|
||
/// <param name="field">字段名称</param>
|
||
/// <param name="width">宽度</param>
|
||
/// <param name="title">标题</param>
|
||
/// <param name="isSort">是否排序</param>
|
||
public void AddHeadCol(string field, string width, string title, bool isSort, bool isDefault = false, string sortDefault = "desc")
|
||
{
|
||
if (isSort)
|
||
{
|
||
var sortClass = "";
|
||
if (isDefault)
|
||
{
|
||
if (sortDefault == "desc")
|
||
{
|
||
sortClass = "tablegrid_sort_desc";
|
||
}
|
||
else
|
||
{
|
||
sortClass = "tablegrid_sort_asc";
|
||
}
|
||
}
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' class='tablegrid_sort " + sortClass + "' width=\"{1}\">{2}<span class='tablegrid-sort-icon'> </span></th>\n", field, width, title);
|
||
colnum++;
|
||
}
|
||
else
|
||
AddHeadCol(field, width, title);
|
||
}
|
||
public void AddHeadCol(string field, string width, string title, string click, bool isSort, bool isDefault = false, string sortDefault = "desc")
|
||
{
|
||
if (isSort)
|
||
{
|
||
var sortClass = "";
|
||
if (isDefault)
|
||
{
|
||
if (sortDefault == "desc")
|
||
{
|
||
sortClass = "tablegrid_sort_desc";
|
||
}
|
||
else
|
||
{
|
||
sortClass = "tablegrid_sort_asc";
|
||
}
|
||
}
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' onClick=\"{3}\" class='tablegrid_sort " + sortClass + "' width=\"{1}\">{2}<span class='tablegrid-sort-icon'> </span></th>\n", field, width, title, click);
|
||
colnum++;
|
||
}
|
||
else
|
||
AddHeadCol(field, width, title);
|
||
}
|
||
public void AddHeadCol(string field, string width, string title, string click, string style)
|
||
{
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' onClick=\"{3}\" style=\"{4}\" class='tablegrid_sort' width =\"{1}\">{2}<span class='tablegrid-sort-icon'> </span></th>\n", field, width, title, click, style);
|
||
colnum++;
|
||
}
|
||
/// <summary>
|
||
/// 添加头部影藏的单元格(作为影藏数据)
|
||
/// </summary>
|
||
/// <param name="field">字段名称</param>
|
||
/// <param name="title">标题(可传空值)</param>
|
||
public void AddHiddenHeadCol(string field, string title)
|
||
{
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' style='display:none'>{1}</th>\n", field, title);
|
||
colnum++;
|
||
}
|
||
/// <summary>
|
||
/// 添加单元格
|
||
/// </summary>
|
||
/// <param name="value">数据值</param>
|
||
public void AddCol(object value)
|
||
{
|
||
temp.AppendFormat(" <td field='{1}'>{0}</td>\n", ((value == null || value.ToString().Trim().Equals("")) ? " " : value), (fields.Count <= 1 ? "" : fields[fieldIndex]));
|
||
fieldIndex++;
|
||
}
|
||
/// <summary>
|
||
/// 添加单元格
|
||
/// </summary>
|
||
/// <param name="value">数据值</param>
|
||
public void AddErrCol(object value)
|
||
{
|
||
temp.AppendFormat(" <td colspan='{1}'>{0}</td>\n", ((value == null || value.ToString().Trim().Equals("")) ? " " : value), fields.Count);
|
||
fieldIndex++;
|
||
}
|
||
/// <summary>
|
||
/// 天津爱影藏单元格
|
||
/// </summary>
|
||
/// <param name="value"></param>
|
||
public void AddHiddenCol(object value)
|
||
{
|
||
|
||
temp.AppendFormat(" <td field='{1}' style='display:none;'>{0}</td>\n", (value == null || value.ToString().Trim().Equals("")) ? " " : value, (fields.Count <= 1 ? "" : fields[fieldIndex]));
|
||
fieldIndex++;
|
||
}
|
||
/// <summary>
|
||
/// 添加单元格
|
||
/// </summary>
|
||
/// <param name="colspan"></param>
|
||
/// <param name="value"></param>
|
||
public void AddCol(string colspan, object value)
|
||
{
|
||
temp.AppendFormat(" <td field='{2}' {1}>{0}</td>\n", (value == null || value.ToString().Trim().Equals("")) ? " " : value, "colspan=" + colspan + "", (fields.Count <= 1 ? "" : fields[fieldIndex]));
|
||
fieldIndex++;
|
||
}
|
||
public void AddCol(string style, string width, object value)
|
||
{
|
||
temp.AppendFormat(" <td field='{3}' style=\"{0}\" width=\"{1}\">{2}</td>\n", style, width, (value == null || value.ToString().Trim().Equals("")) ? " " : value, (fields.Count <= 1 ? "" : fields[fieldIndex]));
|
||
fieldIndex++;
|
||
}
|
||
public void AddCol(string style, string cls, string width, object value)
|
||
{
|
||
temp.AppendFormat(" <td field='{3}' style=\"{0}\" width=\"{1}\" class=\"{4}\">{2}</td>\n", style, width, (value == null || value.ToString().Trim().Equals("")) ? " " : value, (fields.Count <= 1 ? "" : fields[fieldIndex]), cls);
|
||
fieldIndex++;
|
||
}
|
||
/// <summary>
|
||
/// 添加td(有title显示)
|
||
/// </summary>
|
||
/// <param name="value">显示值</param>
|
||
/// <param name="length">vale显示的长度</param>
|
||
/// <param name="title">提示的title</param>
|
||
public void AddCol(string value, int length, string title)
|
||
{
|
||
if (value.Length <= length)
|
||
{
|
||
temp.AppendFormat(" <td field='{1}' title='{2}'>{0}</td>\n", ((value == null || value.ToString().Trim().Equals("")) ? " " : HtmlHelper.NoHTML(value)), (fields.Count <= 1 ? "" : fields[fieldIndex]), HtmlHelper.NoHTML(title));
|
||
}
|
||
else
|
||
{
|
||
value = value.Substring(0, length) + "...";
|
||
temp.AppendFormat(" <td field='{1}' title='{2}'>{0}</td>\n", ((value == null || value.ToString().Trim().Equals("")) ? " " : HtmlHelper.NoHTML(value)), (fields.Count <= 1 ? "" : fields[fieldIndex]), HtmlHelper.NoHTML(title));
|
||
}
|
||
fieldIndex++;
|
||
}
|
||
/// <summary>
|
||
/// 添加数据行
|
||
/// </summary>
|
||
public void AddRow()
|
||
{
|
||
AddRow("");
|
||
}
|
||
|
||
public void AddRow(string attribute)
|
||
{
|
||
rowCout++;
|
||
fieldIndex = 0;
|
||
if (attribute.Trim() == "")
|
||
table.AppendLine(" <tr>");
|
||
else
|
||
table.AppendLine(" <tr " + attribute + ">");
|
||
if (this.isNumber)
|
||
{
|
||
if (this.gridPager != null)
|
||
table.AppendLine(string.Format("<td style='width:25px;'>{0}</td>", (rowCout + (this.gridPager.page - 1) * this.gridPager.rows)));
|
||
|
||
else
|
||
table.AppendLine(string.Format("<td style='width:25px;'>{0}</td>", rowCout));
|
||
}
|
||
if (this.isCheckbox)
|
||
table.AppendLine("<td style='width:25px;'><input type='checkbox' /></td>");
|
||
table.AppendLine(temp.ToString());
|
||
table.AppendLine(" </tr>");
|
||
temp.Remove(0, temp.Length);
|
||
}
|
||
|
||
|
||
public void AddFootRow(string attribute = null)
|
||
{
|
||
fieldIndex = 0;
|
||
if (string.IsNullOrEmpty(attribute))
|
||
foot.AppendLine("<tr>");
|
||
else
|
||
foot.AppendLine("<tr " + attribute + ">");
|
||
if (this.isNumber)
|
||
foot.AppendLine("<td style='width:25px;'> </td>");
|
||
if (this.isCheckbox)
|
||
foot.AppendLine("<td> </td>");
|
||
foot.AppendLine(temp.ToString());
|
||
foot.AppendLine(" </tr>");
|
||
temp.Remove(0, temp.Length);
|
||
}
|
||
/// <summary>
|
||
/// 添加头部行
|
||
/// </summary>
|
||
public void AddHeadRow()
|
||
{
|
||
fieldIndex = 0;
|
||
var trClass = "grid_table_head";
|
||
if (this.isFloatHead) {
|
||
trClass = "grid_table_head grid_table_float_head";
|
||
}
|
||
table.AppendLine("<tr class='"+ trClass + "'>");
|
||
|
||
if (this.isNumber)
|
||
table.AppendLine("<th style='width:25px;'> </th>");
|
||
if (this.isCheckbox)
|
||
table.AppendLine("<th style='width:25px;'><input type='checkbox' ntype='checkAll'/></th>");
|
||
table.AppendLine(temp.ToString());
|
||
table.AppendLine(" </tr>");
|
||
temp.Remove(0, temp.Length);
|
||
table.AppendLine("</thead><tbody>");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取整个表格HTML
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string GetTable()
|
||
{
|
||
table.AppendLine("</tbody></table><div style='width:100%;height:100px;'></div></div>");
|
||
string tmp = table.ToString().Replace("[columns]", columns.Length > 0 ? columns.Substring(0, columns.Length - 1) : "");
|
||
temp = null;
|
||
table = null;
|
||
return tmp;
|
||
}
|
||
/// <summary>
|
||
/// 添加头部分割线
|
||
/// </summary>
|
||
public void AddTHeadAndTbodySplit()
|
||
{
|
||
table = new StringBuilder(table.ToString().Replace("<tbody>", "<thead>") + "</thead><tbody>");
|
||
}
|
||
/// <summary>
|
||
/// 获取表格头部html
|
||
/// </summary>
|
||
/// <param name="tableClass">重写的表格样式</param>
|
||
/// <returns></returns>
|
||
public string GetHead(string tableClass = "layui-table")
|
||
{
|
||
//string tmp = table.ToString().Replace("[columns]", columns.Length > 0 ? columns.Substring(0, columns.Length - 1) : "").Replace("<tbody>", "<thead>").Replace("layui-table", tableClass);
|
||
//tmp += "</thead><tbody></tbody></table><div style='width:100%;height:100px;'></div></div>";
|
||
string tmp = table.ToString().Replace("[columns]", columns.Length > 0 ? columns.Substring(0, columns.Length - 1) : "");
|
||
tmp += "</tbody></table><div style='width:100%;height:100px;'></div></div>";
|
||
temp = null;
|
||
table = null;
|
||
return tmp;
|
||
}
|
||
/// <summary>
|
||
/// 只获取数据行
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string GetRows()
|
||
{
|
||
string tmp = table.ToString();
|
||
temp = null;
|
||
table = null;
|
||
return tmp;
|
||
}
|
||
/// <summary>
|
||
/// 读取数据脚部分
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string GetFoot()
|
||
{
|
||
string footClumn = foot.ToString();
|
||
foot = null;
|
||
return footClumn;
|
||
}
|
||
#endregion
|
||
}
|
||
}
|