Hab den Baum mal mit einem automatischen Numerierungs View und der Angabe
der Tiefe gebastelt

(Anmerkung: die austeigende Laufzahl
in der Datenliste ist keine Tiefenangabe, sondern ein Hilfsmittel zur Sortierung
in der Datenbank. Nur so ist die Reihenfolge beim Select eindeutig gegeben)
Code: Alles auswählen
#database select
#db_category_tree = cursor.fetchall()
db_category_tree = [
[10, 0, 'Dach', 'container', 1],
[20, 0, 'Heizung', 'container', 2],
[101, 10, 'Firstpfette', 'task', 3],
[102, 10, 'Innenverkleidung', 'task', 4],
[201, 20, 'Aussparungen OG', 'task', 5],
[202, 20, 'Radiatoren EG', 'task', 6],
[301, 202, 'Schlafen', 'task', 7],
[302, 202, 'Wohnen', 'task', 8]
]
#create tree_model
tree = {}
tree[0]= {'parent_id': 0, 'name': 'root', 'childs': []}
for row in db_category_tree:
tree[row[0]]= {'parent_id': row[1], 'name': row[2], 'type': row[3], 'childs': []}
tree[row[1]]['childs'].append(row[0])
#create simple_depth_view
def tree_view(tree, n=2):
def recursion(tree,node,depth, index):
if depth +1 <= n:
j = 1
for child in tree[node]['childs']:
tree[child]['depth'] = depth +1
tree[child]['index'] = str(index) + '.' + str(j)
j += 1
for child in tree[node]['childs']:
if tree[child]['depth'] == 1:
print str(tree[child]['index'])[2:], tree[child]['name']
else:
print (tree[child]['depth'] - 1) * ' ' + str(tree[child]['index'])[2:], tree[child]['name']
if tree[child]['childs'] != []:
recursion(tree,child,tree[child]['depth'], tree[child]['index'])
recursion(tree,0,0,0)
Ergebnis:
Code: Alles auswählen
>>> tree_view(tree,1)
1 Dach
2 Heizung
>>> tree_view(tree,2)
1 Dach
1.1 Firstpfette
1.2 Innenverkleidung
2 Heizung
2.1 Aussparungen OG
2.2 Radiatoren EG
>>> tree_view(tree,3)
1 Dach
1.1 Firstpfette
1.2 Innenverkleidung
2 Heizung
2.1 Aussparungen OG
2.2 Radiatoren EG
2.2.1 Schlafen
2.2.2 Wohnen