首页 > 代码库 > python + easyuitree实现

python + easyuitree实现

 前台:

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

后台:

def tree_roledb(request):
    a=[]
    list_items = Roledb.objects.all()
    for p in list_items.values():
        a.append(p)
        print a

//先把取出的数据存入一个列表a

//调用递归
    alist = treeRecursive(a)
    b=json.dumps(alist)
    return HttpResponse(b)
def treeRecursive(data):

//判断循环次数为len(data)
    for s in range(len(data)):
        a = 0//记录for循环次数
        for i in data:
            type(data)
            a=a+1//当前第一次循环
            b = 0//判断是否存在子节点,存在赋值为1,不存在为0
            for j in data:
                if (i[‘id‘] == j[‘parentId‘])://当存在子节点时b=1退出当前for循环进入下一次循环
                    b= 1
                    print i[‘id‘]
                    continue
            if(b==0)://当b==0也就是i这条数据没有子节点时把它加入到父节点中
                for h in data:
                    if(h[‘id‘]==i[‘parentId‘])://是否存在父节点
                        try:
                            h[‘children‘].append(i)//如果当前的父节点存在children这个键,直接append(i)
                        except Exception,e:
                            h[‘children‘]=[]
                            h[‘children‘].append(i)//如果不存在则把新建children键,此键类型为list,并把当前数据加入到父节点中
                        del data[a-1]//删除当前数据
    print data  
    return data

   //最后生成的data为easyui tree所需要的格式

python + easyuitree实现