using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace PMS.BusinessModels.EasyUI
{
///
/// DataGrid的属性
///
public class DataGridModel
{
///
/// 初始化DataGrid对象
///
public DataGridModel()
{
this.ColumnList = new List();
this.TotalRows = 0;
}
///
/// 初始化DataGrid对象
///
/// 数据列名称集合
public DataGridModel(params string[] columns)
{
this.ColumnList = new List();
this.TotalRows = 0;
if (columns != null || columns.Length > 0)
{
foreach (var col in columns)
{
this.ColumnList.Add(new ColumnAttribute(col));
}
}
}
///
/// 初始化DataGrid对象
///
/// 总行数
/// 列集合
public DataGridModel(int totalRows, params string[] columns)
{
this.ColumnList = new List();
if (totalRows < 0)
{
totalRows = 0;
}
this.TotalRows = totalRows;
if (columns != null || columns.Length > 0)
{
foreach (var col in columns)
{
this.ColumnList.Add(new ColumnAttribute(col));
}
}
}
///
/// 初始化DataGrid对象
///
/// 分割字符
/// 数据列与显示列名称的集合,每个都使用自定义的分割字符去分割 每一个为数据列 每二个为显示列
public DataGridModel(char spiltChar, params string[] columns)
{
this.ColumnList = new List();
this.TotalRows = 0;
if (columns != null || columns.Length > 0)
{
foreach (var col in columns)
{
if (!string.IsNullOrEmpty(col))
{
if (!string.IsNullOrEmpty(spiltChar.ToString()))
{
var list = col.Split(spiltChar);
if (list.Length > 1)
{
this.ColumnList.Add(new ColumnAttribute(list[0], list[1]));
}
else
{
this.ColumnList.Add(new ColumnAttribute(col));
}
}
else
{
this.ColumnList.Add(new ColumnAttribute(col));
}
}
}
}
}
///
/// 初始化DataGrid对象
///
/// 总行数
/// 分割字符
/// 数据列与显示列名称的集合,每个都使用自定义的分割字符去分割 每一个为数据列 每二个为显示列
public DataGridModel(int totalRows, char spiltChar, params string[] columns)
{
this.ColumnList = new List();
if (totalRows < 0)
{
totalRows = 0;
}
this.TotalRows = totalRows;
if (columns != null || columns.Length > 0)
{
foreach (var col in columns)
{
if (!string.IsNullOrEmpty(col))
{
if (!string.IsNullOrEmpty(spiltChar.ToString()))
{
var list = col.Split(spiltChar);
if (list.Length > 1)
{
this.ColumnList.Add(new ColumnAttribute(list[0], list[1]));
}
else
{
this.ColumnList.Add(new ColumnAttribute(col));
}
}
else
{
this.ColumnList.Add(new ColumnAttribute(col));
}
}
}
}
}
///
/// 初始化DataGrid对象
///
/// 数据列字段集合
/// 分割字符
public DataGridModel(string columns, char spiltChar)
{
this.ColumnList = new List();
this.TotalRows = 0;
if (string.IsNullOrEmpty(columns) || string.IsNullOrEmpty(spiltChar.ToString()))
{
return;
}
var list = columns.Split(spiltChar);
foreach (var col in list)
{
this.ColumnList.Add(new ColumnAttribute(col));
}
}
///
/// 初始化DataGrid对象
///
/// 总行数
/// 数据列字段集合
/// 分割字符
public DataGridModel(int totalRows, string columns, char spiltChar)
{
this.ColumnList = new List();
if (totalRows < 0)
{
totalRows = 0;
}
this.TotalRows = totalRows;
if (string.IsNullOrEmpty(columns) || string.IsNullOrEmpty(spiltChar.ToString()))
{
return;
}
var list = columns.Split(spiltChar);
foreach (var col in list)
{
this.ColumnList.Add(new ColumnAttribute(col));
}
}
///
/// 添加列
///
///
public void AddColumn(string column)
{
if (this.ColumnList == null)
{
this.ColumnList = new List();
}
if (string.IsNullOrEmpty(column))
{
return;
}
this.ColumnList.Add(new ColumnAttribute(column));
}
///
/// 添加列
///
/// 数据列名称
/// 显示列名称
public void AddColumn(string dataColumn, string textColumn)
{
if (this.ColumnList == null)
{
this.ColumnList = new List();
}
if (string.IsNullOrEmpty(dataColumn) && string.IsNullOrEmpty(textColumn))
{
return;
}
if (string.IsNullOrEmpty(dataColumn))
{
dataColumn = textColumn;
}
if (string.IsNullOrEmpty(textColumn))
{
textColumn = dataColumn;
}
this.ColumnList.Add(new ColumnAttribute(dataColumn, textColumn));
}
///
/// 总行数
///
public int TotalRows { get; set; }
///
/// 消耗时间
///
private double WasteTime = 0.01;
///
/// 查询消耗时间
///
public double SetWasteTime
{
set
{
if (value < 0.01)
{
this.WasteTime = 0.01;
}
else
{
this.WasteTime = value;
}
}
}
///
/// 列名称集合
///
private List ColumnList;
///
/// 基础数据表
///
private DataTable basicTable { get; set; }
///
/// 设置数据表
///
public DataTable SetDataTable
{
set
{
if (value == null || value.Rows.Count == 0)
{
return;
}
this.basicTable = value.Copy();
}
}
///
/// 通过DataTable转成对应的Json格式字段串
///
///
public string ToJsonString()
{
if (this.basicTable == null || this.basicTable.Rows.Count == 0 || this.ColumnList.Count == 0)
{
return "{\"total\":0,\"ctime\":" + this.WasteTime + ",\"rows\":[]}";
}
StringBuilder sb = new StringBuilder();
int total = this.TotalRows;//获取是否设置了总行数
if (total == 0)
{
total = this.basicTable.Rows.Count;
}
sb.Append("{\"total\":" + total + ",\"ctime\":" + this.WasteTime + ",\"rows\":[");
var colList = this.ColumnList;
foreach (DataRow dr in this.basicTable.Rows)
{
sb.Append("{");//一行的开始
foreach (var col in colList)
{
//判断数据库列是否存在
if (this.basicTable.Columns.Contains(col.DataColumn))
{
var value = dr[col.DataColumn].ToString().Trim().Replace("\r", "\\r").Replace("\n", "\\n");
sb.Append("\"" + col.TextColumn + "\":\"" + value + "\",");
}
else
{
sb.Append("\"" + col.TextColumn + "\":\"\",");
}
}
sb.Remove(sb.Length - 1, 1);//移除最后一个逗号
sb.Append("},");//一行的结束
}
sb.Remove(sb.Length - 1, 1);//去除最后的逗号
sb.Append("]}");//一行的结束
return sb.ToString();
}
}
}