DataTable 转化成EasyUiTree Json格式
DataTable dt = new DataTable();
DataColumn d1 = new DataColumn("id", System.Type.GetType("System.Int32"));
DataColumn d2 = new DataColumn("text", System.Type.GetType("System.String"));
DataColumn d3 = new DataColumn("Icon", System.Type.GetType("System.String"));
DataColumn d4 = new DataColumn("ParentId", System.Type.GetType("System.Int32"));
DataColumn d5 = new DataColumn("state", System.Type.GetType("System.String"));
dt.Columns.Add(d1);
dt.Columns.Add(d2);
dt.Columns.Add(d3);
dt.Columns.Add(d4);
dt.Columns.Add(d5);
DataRow row1 = dt.NewRow();
row1["id"] = 1;
row1["text"] = "系统管理员";
row1["Icon"] = "";
row1["ParentId"] = 0;
row1["state"] = "open";
dt.Rows.Add(row1);
DataRow row2 = dt.NewRow();
row2["id"] = 2;
row2["text"] = "用户列表";
row2["Icon"] = "";
row2["ParentId"] = 1;
row2["state"] = "open";
dt.Rows.Add(row2);
DataRow row3 = dt.NewRow();
row3["id"] = 3;
row3["text"] = "添加用户权限";
row3["Icon"] = "";
row3["ParentId"] = 2;
row3["state"] = "open";
dt.Rows.Add(row3);
string sss = TabletoEasyJson(dt, "ParentId", "0", "Id", "text");
if (sss.Length > 12)
sss = sss.Substring(12);
context.Response.Write(sss);
public static string TabletoEasyJson(DataTable dt, string ParentId, string pvalue, string id, string text)
{
StringBuilder sb = new StringBuilder();
string filter = String.Format(" {0}='{1}' ", ParentId, pvalue);//获取顶级目录;//获取顶级目录);
DataRow[] drows = dt.Select(filter);
if (drows.Length < 1)
return "";
sb.Append(",\"children\":[");
foreach (DataRow dr in drows)
{
string pcv = dr[id].ToString();
sb.Append("{");
sb.AppendFormat("\"id\":\"{0}\",", dr[id].ToString());
sb.AppendFormat("\"text\":\"{0}\",", dr[text].ToString());
sb.AppendFormat("\"state\":\"{0}\"",dr["state"].ToString());
sb.Append(TabletoEasyJson(dt, ParentId, pcv, id, text).TrimEnd(','));
sb.Append("},");
}
if (sb.ToString().EndsWith(","))
{
sb.Remove(sb.Length - 1, 1);
}
sb.Append("]");
return sb.ToString();
}