将数据处理成easyui控件需要的格式
Posted Coder_小菜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将数据处理成easyui控件需要的格式相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using Newtonsoft.Json; using Newtonsoft.Json.Converters; namespace Original.Common { /// <summary> /// 将数据转换成easyui控件支持的数据格式 /// </summary> public static class JsonHelper { /// <summary> /// 将DataTable转换成Combobox支持的数据格式 /// </summary> /// <param name="dt">数据源DataTable</param> /// <param name="valueField">id字段名称</param> /// <param name="textField">text字段名称</param> /// <returns>json</returns> public static string DataTableToCombobox(DataTable dt, string valueField, string textField) { List<Combobox> lst = new List<Combobox>(); foreach (DataRow row in dt.Rows) { if (row[valueField] == DBNull.Value || row[textField] == DBNull.Value) { continue; } lst.Add(new Combobox() { id = row[valueField].ToString(), text = row[textField].ToString() }); } string json = JsonConvert.SerializeObject(lst); return json; } /// <summary> /// 将实体转换成json /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="t">实体实例</param> /// <returns>json</returns> public static string ModelToJson<T>(T t) { string json = JsonConvert.SerializeObject(t); return json; } /// <summary> /// DataTable转换成DataGrid数据格式 /// </summary> /// <param name="dt">数据源DataTable</param> /// <param name="iRecordCount">数据总记录数</param> /// <returns>json</returns> public static string DataTableToDataGrid(DataTable dt, int iRecordCount) { DataGrid dg = new DataGrid(); dg.total = iRecordCount.ToString(); dg.rows = dt; string dtJson = JsonConvert.SerializeObject(dg, new JsonConverter[] { new DataTableConverter() }); return dtJson; } /// <summary> /// DataTable转换成Combotree数据格式 /// </summary> /// <param name="dt">数据源DataTable</param> /// <param name="parentValue">一级节点的父ID值</param> /// <param name="idField">id字段名称</param> /// <param name="parentField">父字段名称</param> /// <param name="textField">显示值的字段名称</param> /// <returns>json</returns> public static string DataTableToCombotree(DataTable dt, string parentValue, string idField, string parentField, string textField) { List<Combotree> lstCombotree = new List<Combotree>(); BindTree(dt, null, parentValue, lstCombotree, idField, parentField, textField); string dtJson = JsonConvert.SerializeObject(lstCombotree); return dtJson; } #region 辅助方法 /// <summary> /// 构造Combotree结构数据 /// </summary> /// <param name="dtSource">DataTable数据源</param> /// <param name="parentNode">父结点</param> /// <param name="parentID">一级节点的父ID</param> /// <param name="ListCombotree">Combotree空列表</param> private static void BindTree(DataTable dtSource, Combotree parentNode, string parentID, List<Combotree> ListCombotree, string idField, string parentField, string textField) { DataRow[] rows = dtSource.Select(string.Format("{0}=‘{1}‘", parentField, parentID)); foreach (DataRow row in rows) { if (row[idField] == DBNull.Value || row[textField] == DBNull.Value) { continue; } Combotree node = new Combotree(); node.text = row[textField].ToString(); node.id = row[idField].ToString(); BindTree(dtSource, node, row[idField].ToString(), ListCombotree, idField, parentField, textField); if (parentNode == null) //(row[arg.parentid].ToString() == "-1") { // 一级数据 ListCombotree.Add(node); } else { // 子级数据 parentNode.children.Add(node); } } } #endregion } #region easyui 控件结构 /// <summary> /// Combobox /// </summary> [Serializable] public class Combobox { /// <summary> /// 值 /// </summary> public string id { get; set; } /// <summary> /// 显示的名称 /// </summary> public string text { get; set; } } /// <summary> /// Combobox /// </summary> [Serializable] public class Combotree { /// <summary> /// value的值 /// </summary> public string id { get; set; } /// <summary> /// text显示的值 /// </summary> public string text { get; set; } /// <summary> /// 子列表 /// </summary> public List<Combotree> children { get; set; } } /// <summary> /// DataGrid /// </summary> [Serializable] public class DataGrid { /// <summary> /// 数据总记录 /// </summary> public string total { get; set; } /// <summary> /// 数据源DataTable /// </summary> public DataTable rows { get; set; } } #endregion }
以上是关于将数据处理成easyui控件需要的格式的主要内容,如果未能解决你的问题,请参考以下文章
C#将datatable生成easyui的绑定tree 的json数据格式
jquery easyui中日历控件(easyui-datebox) 现实的时间格式为:06/27/2013 如图: