Asp.net与ExtJs结合,当有三层时就不显示树了,望高手讲解
/// <summary>
/// 加载树
/// </summary>
/// <returns></returns>
///
StringBuilder jsonSource = null;
public ActionResult doTreeLoad()
{
string FunSql = "SELECT * FROM [FUNCTION]";
DataTable Fun = SQLHelp.GetDataTable(new SQLParams(FunSql, null));
List<Function> FunList = new List<Function>();
for (int i = 0; i < Fun.Rows.Count; i++)
{
Function fc = new Function();
fc.Id = Convert.ToInt32(Fun.Rows[i]["id"]);
fc.Name = Fun.Rows[i]["name"].ToString();
fc.ParentId = Convert.ToInt32(Fun.Rows[i]["parentId"].ToString());
fc.Action = Fun.Rows[i]["action"].ToString();
fc.Remark = Fun.Rows[i]["remark"].ToString();
FunList.Add(fc);
}
string deptStr = "[";
if (FunList.Count > 0)
{
foreach (Function d in FunList)
{
if (d.ParentId == 0)
{
jsonSource = new StringBuilder();
RecursionDept(FunList, d);
deptStr += ModifyStr(jsonSource.ToString());
deptStr += ",";
}
}
deptStr += "]";
}
else
{
deptStr += ",]";
}
deptStr = ModifyStr(deptStr);
Response.Write(deptStr);
Response.End();
Response.Clear();
return null;
}
private void RecursionDept(List<Function> deptList, Function dt)
{
if (HasChild(deptList, dt))
{
jsonSource.Append("{id:" + dt.Id + ",");
jsonSource.Append(@"text:'" + dt.Name + @"',");
jsonSource.Append("children:[");
List<Function> deptSub = GetChild(deptList, dt);
foreach (Function d in deptSub)
{
RecursionDept(deptList, d);
}
jsonSource.Append("]}");
}
else
{
jsonSource.Append("{id:" + dt.Id + ",");
jsonSource.Append(@"text:'" + dt.Name + @"',");
jsonSource.Append("leaf:true},");
}
}
/// <summary>
/// 修改字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private string ModifyStr(string str)
{
return str.Replace(",]", "]");
}
/// <summary>
/// 判断是否包含字节点
/// </summary>
/// <param name="deptQuery"></param>
/// <param name="dt"></param>
/// <returns></returns>
private bool HasChild(List<Function> deptQuery, Function dt)
{
return GetChild(deptQuery, dt).Count > 0 ? true : false;
}
private List<Function> GetChild(List<Function> deptQuery, Function dt)
{
List<Function> deptSub = new List<Function>();
foreach (Function d in deptQuery)
{
if (d.ParentId == dt.Id)
{
deptSub.Add(d);
}
}
return deptSub;
}
当有两层时显示正确,三层时就不对了,比如A下面有B下面有C,当C下面再有子节点就不显示树了,望高手讲解。