EasyUI TreeJson

单于山
2023-12-01

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;
        }

 

转载于:https://www.cnblogs.com/yimeishui/p/5902522.html

 类似资料:

相关阅读

相关文章

相关问答