Python + easyuitree Implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.