- 论坛徽章:
- 0
|
"""
在Oracle下面的一个connect by 语句,形成一个树的数据结构
select level,connect_by_isleaf,t_dept.companyid,t_dept.companyname,t_dept.parentcompanyid from t_dept start with parentcompanyid is null connect by prior companyid = parentcompanyid
取出数据: data = oracle_cur.fetchall()
"""
# 生成一个Json格式的树结构
def tree(tree_set):
"""recieve record struct: [level,isleaf,thisid,thisname,parentid]"""
rem_node = {}
tree = []
for level,isleaf,thisid,thisname,parentid in tree_set:
if isleaf == 0 :
node = {"id":thisid,"text":unicode(thisname,'gbk'),'children':[]}
else :
node = {"id":thisid,"text":unicode(thisname,'gbk'),'leaf':True}
rem_node.update({str(thisid):node})
if level == 1:
tree.append(node)
else :
rem_node[str(parentid)]['children'].append(node)
print isleaf,thisid,thisname,parentid
#return tree
return cjson.encode(tree)
if __name__ == '__main__':
tt = tree(data) #可以找一个支持json数据格式的javascript树控件,记得当时是用Jquery做的。
print tt |
|