递归:python+easyui实现tree

Posted yanhuaqiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归:python+easyui实现tree相关的知识,希望对你有一定的参考价值。

后台部分:
@csrf_exempt
def getTree(request):
row = serializers.serialize("json", models.Tree.objects.all())
rows = []
for r in json.loads(row):
ra = r[‘fields‘]
rows.append(ra)
tree = tree_data(‘0‘, rows)
print(tree)
return HttpResponse(json.dumps(tree), content_type="application/json")


def tree_data(pid, tree_list):
tree = []
for t in tree_list:
if pid == str(t[‘pid‘]):
children_map = {}
domain_id = str(t[‘num‘])
pid = str(t[‘pid‘])
name = t[‘text‘]
children_map[‘id‘] = domain_id
children_map[‘text‘] = name
children_map[‘state‘] = ‘closed‘
tree.append(children_map)
children_map[‘children‘] = tree_data(domain_id, tree_list)
return tree

页面:
<script type="text/javascript">

$(document).ready(function () {
$.ajax({
url:‘/getTree/‘,
type:‘post‘,
dataType:‘json‘,
success:function(data){
console.log(data);
$(‘#tt‘).tree({
data:
data
,
onClick : function (node) {
console.log(node);
var text = node[‘text‘]
console.log(text);
if ($("#tab").tabs(‘exists‘, text)) {
$(‘#tab‘).tabs(‘select‘, text);
} else {
$(‘#tab‘).tabs(‘add‘, {
title : text,
closable : true,
content : text
});
}
}
});
},
error: function (jqXHR, textStatus, errorThrown) {
alert(失败!‘);
}
});
})
</script>
 






























































以上是关于递归:python+easyui实现tree的主要内容,如果未能解决你的问题,请参考以下文章

Python之函数参数局部全局变量前向引用递归

递归转迭代实操记录

递归选中easyui树

Python3基础语法小结与简单编程实操案例

easyUI常用API

#私藏项目实操分享#Python游戏开发,pgzrun模块,Python实现阿肯色克隆人游戏