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; /// /// 默认为居左padding-left:5px /// public string tdLeft5 = "text-align:left;padding-left:5px;"; /// /// 默认为居左padding:5px; /// public string tdLeftPadding5 = "text-align:left;padding:5px;"; /// /// 是否显示复选框(默认为false) /// public bool isCheckbox { get; set; } /// /// 是否显示行号(默认为true) /// public bool isNumber { get; set; } /// /// 分页信息 /// public Pager gridPager { get; set; } #region 表格操作 /// /// 创建有ID的table /// /// 表的ID public Table(string TableId) { this.isCheckbox = false; this.isNumber = true; table.AppendLine(string.Format("", TableId)); } /// /// 创建有ID的table 和表格样式 /// /// /// public Table(string TableId, string tableClass) { this.isCheckbox = false; this.isNumber = true; table.AppendLine(string.Format("
", TableId, tableClass)); } /// /// 创建空表(用于只显示数据列) /// /// 字段列表(从全段返回) /// 是没表头 public Table(string columns, bool isNoHead) { this.isCheckbox = false; this.isNumber = true; string[] nfields = columns.Split(','); foreach (string item in nfields) { fields.Add(item); } } /// /// 添加头部单元格 /// /// 字段名称 /// 宽度 /// 标题 public void AddHeadCol(string field, string width, string title) { fields.Add(field); columns += field + ","; temp.AppendFormat(" \n", field, width, title); colnum++; } /// /// 添加头部单元格 /// /// 字段名称 /// 合并列 /// 标题 public void AddHeadCol(string field, int colspan, string title) { fields.Add(field); columns += field + ","; temp.AppendFormat(" \n", field, colspan, title); colnum++; } public void AddHeadColRowSpan(string field, int rowspan, string title) { fields.Add(field); columns += field + ","; temp.AppendFormat(" \n", field, rowspan, title); colnum++; } /// /// 添加头部单元格 /// /// 字段名称 /// 宽度 /// 标题 /// 是否排序 public void AddHeadCol(string field, string width, string title, bool isSort) { if (isSort) { fields.Add(field); columns += field + ","; temp.AppendFormat(" \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(" \n", field, width, title, click, style); colnum++; } /// /// 添加头部影藏的单元格(作为影藏数据) /// /// 字段名称 /// 标题(可传空值) public void AddHiddenHeadCol(string field, string title) { fields.Add(field); columns += field + ","; temp.AppendFormat(" \n", field, title); colnum++; } /// /// 添加单元格 /// /// 数据值 public void AddCol(object value) { temp.AppendFormat(" \n", ((value == null || value.ToString().Trim().Equals("")) ? " " : value), (fields.Count <= 1 ? "" : fields[fieldIndex])); fieldIndex++; } /// /// 天津爱影藏单元格 /// /// public void AddHiddenCol(object value) { temp.AppendFormat(" \n", (value == null || value.ToString().Trim().Equals("")) ? " " : value, (fields.Count <= 1 ? "" : fields[fieldIndex])); fieldIndex++; } /// /// 添加单元格 /// /// /// public void AddCol(string colspan, object value) { temp.AppendFormat(" \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(" \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(" \n", style, width, (value == null || value.ToString().Trim().Equals("")) ? " " : value, (fields.Count <= 1 ? "" : fields[fieldIndex]), cls); fieldIndex++; } /// /// 添加td(有title显示) /// /// 显示值 /// vale显示的长度 /// 提示的title public void AddCol(string value, int length, string title) { if (value.Length <= length) { temp.AppendFormat(" \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(" \n", ((value == null || value.ToString().Trim().Equals("")) ? " " : HtmlHelper.NoHTML(value)), (fields.Count <= 1 ? "" : fields[fieldIndex]), HtmlHelper.NoHTML(title)); } fieldIndex++; } /// /// 添加数据行 /// public void AddRow() { AddRow(""); } public void AddRow(string attribute) { rowCout++; fieldIndex = 0; if (attribute.Trim() == "") table.AppendLine(" "); else table.AppendLine(" "); if (this.isNumber) { if (this.gridPager != null) table.AppendLine(string.Format("", (rowCout + (this.gridPager.page - 1) * this.gridPager.rows))); else table.AppendLine(string.Format("", rowCout)); } if (this.isCheckbox) table.AppendLine(""); table.AppendLine(temp.ToString()); table.AppendLine(" "); temp.Remove(0, temp.Length); } public void AddFootRow(string attribute = null) { fieldIndex = 0; if (string.IsNullOrEmpty(attribute)) foot.AppendLine(""); else foot.AppendLine(""); if (this.isNumber) foot.AppendLine(""); if (this.isCheckbox) foot.AppendLine(""); foot.AppendLine(temp.ToString()); foot.AppendLine(" "); temp.Remove(0, temp.Length); } /// /// 添加头部行 /// public void AddHeadRow() { fieldIndex = 0; table.AppendLine(""); if (this.isNumber) table.AppendLine(""); if (this.isCheckbox) table.AppendLine(""); table.AppendLine(temp.ToString()); table.AppendLine(" "); temp.Remove(0, temp.Length); } /// /// 获取整个表格HTML /// /// public string GetTable() { table.AppendLine("
{2}{2}{2}{2} {2} {1}{0}{0}{0}{2}{2}{0}{0}
{0}{0}
  
 
"); string tmp = table.ToString().Replace("[columns]", columns.Length > 0 ? columns.Substring(0, columns.Length - 1) : ""); temp = null; table = null; return tmp; } /// /// 添加头部分割线 /// public void AddTHeadAndTbodySplit() { table = new StringBuilder(table.ToString().Replace("", "") + ""); } /// /// 获取表格头部html /// /// 重写的表格样式 /// public string GetHead(string tableClass = "bas_datagrid_table") { string tmp = table.ToString().Replace("[columns]", columns.Length > 0 ? columns.Substring(0, columns.Length - 1) : "").Replace("", "").Replace("bas_datagrid_table", tableClass); tmp += ""; temp = null; table = null; return tmp; } /// /// 只获取数据行 /// /// public string GetRows() { string tmp = table.ToString(); temp = null; table = null; return tmp; } /// /// 读取数据脚部分 /// /// public string GetFoot() { string footClumn = foot.ToString(); foot = null; return footClumn; } #endregion } }