Front desk:
<div id= "Lowediv" align= "Center" >
<ul id= "Trees" class= "Easyui-tree" >
</ul>
</div>
$ (' #trees '). Tree ({
URL: ",
Loadfilter:function (data) {
if (DATA.D) {
return DATA.D;
} else {
return data;
}
}
});
Background:
def tree_roledb (Request):
A=[]
List_items = Roledb.objects.all ()
For P in List_items.values ():
A.append (P)
Print a
First put the extracted data into a list a
Call recursion
Alist = Treerecursive (a)
B=json.dumps (alist)
return HttpResponse (b)
def treerecursive (data):
Determine the number of loops for Len (data)
for s in range (len (data)):
A = 0//record for loop count
For I in data:
Type (data)
a=a+1//Current First cycle
b = 0//Determine if there are child nodes, there is an assignment of 1, there is no 0
For j in Data:
if (i[' id ') = = j[' ParentID '])://When a child node is present b=1 exits the current for loop into the next loop
b= 1
Print i[' id ']
Continue
if (b==0)://When b==0 is the data that has no child nodes, add it to the parent node
For h in data:
if (h[' id ']==i[' parentid ')://Whether there is a parent node
Try
h[' Children '].append (i)//if the current parent node exists children this key, direct append (i)
Except Exception,e:
h[' Children ']=[]
h[' Children '].append (i)//if not present the new children key, this key type is list, and the current data is added to the parent node
Del data[a-1]//Delete current data
Print data
Return data
The last data generated is the format required for the Easyui tree
Python + easyuitree Implementation