323 lines
13 KiB
C#
323 lines
13 KiB
C#
using System.Collections;
|
||
using System.Text;
|
||
using WX.CRM.Common;
|
||
|
||
namespace Core.Web.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>
|
||
/// 分页信息
|
||
/// </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;
|
||
table.AppendLine(string.Format("<table id='{0}' state='false' cellspacing='0' cellpadding='0' class='layui-table' columns='[columns]'><thead>", 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("<table id='{0}' state='false' cellspacing='0' cellpadding='0' class='{1}' columns='[columns]'><thead>", 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>
|
||
/// 添加头部单元格
|
||
/// </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>
|
||
/// 添加头部单元格
|
||
/// </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++;
|
||
}
|
||
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)
|
||
{
|
||
if (isSort)
|
||
{
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' class='tablegrid_sort' 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, string style)
|
||
{
|
||
fields.Add(field);
|
||
columns += field + ",";
|
||
temp.AppendFormat(" <th field='{0}' onClick=\"{3}\" style=\"{4}\" 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 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;
|
||
table.AppendLine("<tr class='grid_table_head'>");
|
||
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);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取整个表格HTML
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public string GetTable()
|
||
{
|
||
table.AppendLine("</thead></table>");
|
||
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 = "bas_datagrid_table")
|
||
{
|
||
string tmp = table.ToString().Replace("[columns]", columns.Length > 0 ? columns.Substring(0, columns.Length - 1) : "").Replace("<tbody>", "<thead>").Replace("bas_datagrid_table", tableClass);
|
||
tmp += "</thead><tbody></tbody></table>";
|
||
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
|
||
}
|
||
} |