/// <summary>
/// 表格Json数据对象<seealso cref="EosDataGrid"/>
/// </summary>
public class DataGridJson : IJsonObject
{
public int Totals { get; set; }
public DataTable Data { get; set; }
public ColumnConfigData Action { get; set; }
public string Id { get; set; }
public string ToJson()
{
StringBuilder sb = new StringBuilder();
JsonWriter writer = new JsonWriter(sb);
writer.WriteObjectStart();
writer.WritePropertyName("total");
writer.Write(this.Totals);
writer.WritePropertyName("rows");
writer.WriteArrayStart();
foreach (DataRow row in this.Data.Rows)
{
writer.WriteObjectStart();
foreach (DataColumn col in this.Data.Columns)
{
writer.WritePropertyName(col.ColumnName);
writer.Write(row[col]);
}
if (this.Action != null)
{
writer.WritePropertyName(this.Action.Name);
string pk = row["pkey"].ToString();
string editUrl = "";
editUrl = WebHelper.AppendUrl(editUrl, "PK", pk);
StringBuilder html = new StringBuilder();
string[] titles = this.Action.Title.Split(',');
string[] actions = this.Action.Name.Split(',');
for (int i = 0; i < titles.Length; i++)
{
if (actions[i].ToLower() == "edit")
{
html.AppendFormat("<a href=\"javascript:eosDataGrid_OnEdit('{0}','{2}')\" title=\"{1}\">{1}</a>",
this.Id, titles[i], editUrl);
}
else if (actions[i].ToLower() == "delete")
{
html.AppendFormat("<a href=\"javascript:eosDataGrid_OnDelete('{0}','{2}')\" title=\"{1}\">{1}</a>",
this.Id, titles[i], pk);
}
else
{
html.AppendFormat("<a href=\"javascript:event_{0}_{1}('{0}','{3}')\" title=\"{2}\">{2}</a>",
this.Id, actions[i], titles[i], pk);
}
if (html.Length > 0) sb.Append(" ");
}
writer.Write(html.ToString());
}
writer.WriteObjectEnd();
}
writer.WriteArrayEnd();
writer.WriteObjectEnd();
return sb.ToString();
}
}
/// <summary>
/// 表格Json数据提供者<seealso cref="EosDataGrid"/>
/// </summary>
public class DataGridJsonProvider : JsonProvider<DataGridConfigData, DataGridJson>
{
/// <summary>
/// 初始化
/// </summary>
/// <param name="config">用于Provider的配置对象<see cref="DataGridConfigData"/></param>
public DataGridJsonProvider(DataGridConfigData config)
: base(config)
{ }
/// <summary>
/// 返回的Json对象
/// </summary>
/// <param name="ctlId">组件ID</param>
/// <param name="appName">应用名(DB)</param>
/// <returns>Provider返回的<see cref="DataGridJson"/>Json对象</returns>
public override DataGridJson GetJson(string ctlId, string appName, HttpRequest request)
{
DataAccess da = DataAccessFactory.CreateDataAccess(appName);
int configRows = this.Config.GetPropertyValue<int>("pagesize", 10);
int rows = WebHelper.GetParamValue<int>(request, "rows", configRows);
int page = WebHelper.GetParamValue<int>(request, "page", 1);
string table = this.Config.GetPropertyValue(EosDataGrid.TableProperty);
string keys = this.Config.GetPropertyValue(EosDataGrid.PkeyProperty);
string fields = string.Empty;
ColumnConfigData action = null;
foreach (ColumnConfigData col in this.Config.Columns)
{
if (col.Type.ToLower() == "data")
{
fields += "," + col.Name;
}
else if (col.Type.ToLower() == "action")
{
action = col;
}
}
if (fields.Length > 0) fields = fields.Substring(1);
string wheres = WebHelper.AcReplace(this.Config.GetPropertyValue(EosDataGrid.WhereProperty), request.QueryString);
string orders = this.Config.GetPropertyValue(EosDataGrid.OrderbyProperty);
var filterPS = request.QueryString["PS"];
if (!string.IsNullOrEmpty(filterPS))
{
}
int outRows, outPages;
DataTable dataTable = ObjectFactory.SelectDataTable(da, rows, page, table, keys, fields, wheres, orders, out outRows, out outPages);
DataGridJson json = new DataGridJson();
json.Totals = outRows;
json.Data = dataTable;
json.Action = action;
json.Id = ctlId;
return json;
}
}
}