首页 > 代码库 > EasyUI TreeJson

EasyUI TreeJson

1. TreeJson

str = GetTreeJsonByTable(dt, "id", "fid", "0");
      StringBuilder treeResult = new StringBuilder();        StringBuilder treesb = new StringBuilder();        public string GetTreeJsonByTable(DataTable tabel, string idCol, string rela, object pId)        {            string treeJson = string.Empty;            treeResult.Append(treesb.ToString());            treesb.Clear();            if (tabel.Rows.Count > 0)            {                treesb.Append("[");                string filer = string.Empty;                if (pId.ToString() == "")                {                    filer = string.Format("{0} is null", rela);                }                else                {                    filer = string.Format("{0}=‘{1}‘", rela, pId);                }                DataRow[] rowtote = tabel.Select(filer);                if (rowtote.Length > 0)                {                    //foreach (DataRow row in rows)                    for (int i = 0; i < (rowtote.Length); i++)                    {                        treesb.Append("{");                        for (int j = 0; j < tabel.Columns.Count; j++)                        {                            treesb.Append("\"");                            treesb.Append(tabel.Columns[j].ColumnName);                            treesb.Append("\":\"");                            treesb.Append(rowtote[i][j].ToString());                            treesb.Append("\",");                        }                        treesb.Remove(treesb.Length - 1, 1);                        if (tabel.Select(string.Format("{0}=‘{1}‘", rela, rowtote[i][idCol])).Length > 0)                        {                            treesb.Append(",\"children\":");                            GetTreeJsonByTable(tabel, idCol, rela, rowtote[i][idCol]);                            treeResult.Append(treesb.ToString());                            treesb.Clear();                        }                        treeResult.Append(treesb.ToString());                        treesb.Clear();                        treesb.Append("},");                    }                    treesb = treesb.Remove(treesb.Length - 1, 1);                }                treesb.Append("]");                treeResult.Append(treesb.ToString());                treeJson = treeResult.ToString();                treesb.Clear();            }            return treeJson;        }

 

EasyUI TreeJson