use python to return json data of extjs treepanel -
i use python (django) extjs4.2, don't know how return json format data of extjs treepanel below:
[{ "text":"a", "id": 1, "leaf":false, "parentid":0, "root":4, "children": [{ "text":"a_1", "id":2, "leaf":false, "parentid":1, "root":3, "children": [{ "id":7, "leaf":true, "parentid":2, "root":3, "text":"a_1_1", "children":[] }] }] }]
please me, thanks.
the structure of tree pretty easy understand: it's nested list of node every node's children property tree self. 1 can represent follow:
class node(object): def __init__(self, id, text, root='', parent=none): self.text = text self.id = id self.root = root self.parent = parent self.children = [] def append_child(self, node): if node not in self.children: node.parent = self self.children.append(node) def remove_child(self, node): if node in self.children: node.parent = none self.children.remove(node) def parent_id(self): return self.parent.id if self.parent not none else 0 def is_leaf(self): return len(self.children) == 0 def to_dict(self): children_dict = [child.to_dict() child in self.children] return { "id": self.id, "root": self.root, "text": self.text, "leaf": self.is_leaf(), "parentid": self.parent_id(), "children": children_dict, } def to_json(self): return json.dumps(self.to_dict)
Comments
Post a Comment