public
string
GetDepartmentTree()
{<br>
List<department> queryDepart=_departmentService.LoadEntities(u=>
true
).ToList<department>();
string
departmentTree=
null
;
foreach
(department d
in
queryDepart)
{
if
(d.Parent_Department_ID == -1)
{
departmentTree=
"\"id\":"
+d.Department_ID+
",\"text\":\""
+d.Department_Name+
"\","
;
<br>
string
child = FindChild(d.Department_ID, queryDepart);
if
(child !=
null
)
{
departmentTree += child;
}
departmentTree =
"[{"
+ departmentTree +
"}]"
;
}
}
return
departmentTree;
}
private
string
FindChild(
int
id, List<department> queryDepart)
{
bool
flag =
false
;
string
departmentChild =
null
;
foreach
(department d
in
queryDepart)
{
string
anotherChild =
null
;
if
(d.Parent_Department_ID == id)
{<br>
anotherChild =
"\"id\":"
+ d.Department_ID +
",\"text\":\""
+ d.Department_Name +
"\","
;
<br>
string
child = FindChild(d.Department_ID, queryDepart);
if
(child !=
null
)
{
anotherChild = anotherChild + child ;
}
if
(anotherChild[anotherChild.Length - 1] ==
','
)
{
anotherChild = anotherChild.Remove(anotherChild.Length - 1);
}
anotherChild =
"{"
+ anotherChild +
"}"
;
departmentChild += anotherChild+
","
;
}
else
{
flag =
false
;
}
}
if
(departmentChild !=
null
)
{
departmentChild = departmentChild.Remove(departmentChild.Length - 1);
departmentChild =
"\"children\":["
+ departmentChild +
"]"
;
}
return
departmentChild;
}