diff --git a/dsRag/Start.py b/dsRag/Start.py index 1c8f2d0d..37557279 100644 --- a/dsRag/Start.py +++ b/dsRag/Start.py @@ -177,20 +177,34 @@ async def get_tree_data(): FROM knowledge_points ORDER BY parent_id, id """) - result = await cur.fetchall() + rows = await cur.fetchall() - data = [{ - "id": row[0], - "name": row[1], # 确保包含name字段 - "title": row[1], - "parent_id": row[2] if row[2] is not None else 0, - "isParent": not row[3], - "prerequisite": row[4], - "related": row[5], - "open": True - } for row in result] + # 构建节点映射 + nodes = {} + for row in rows: + nodes[row[0]] = { + "id": row[0], + "title": row[1], + "parent_id": row[2] if row[2] is not None else 0, + "isParent": not row[3], + "prerequisite": row[4], + "related": row[5], + "open": True + } - return {"code": 0, "data": data} + # 构建树形结构 + tree_data = [] + for node_id, node in nodes.items(): + parent_id = node["parent_id"] + if parent_id == 0: + tree_data.append(node) + else: + if parent_id in nodes: + if "children" not in nodes[parent_id]: + nodes[parent_id]["children"] = [] + nodes[parent_id]["children"].append(node) + + return {"code": 0, "data": tree_data} except Exception as e: return {"code": 1, "msg": str(e)} diff --git a/dsRag/static/tree.html b/dsRag/static/tree.html index 741d4fc2..99e9de47 100644 --- a/dsRag/static/tree.html +++ b/dsRag/static/tree.html @@ -3,91 +3,126 @@ 知识点树形展示 - - - - - -
- -
- -
- - - - - - - - - -
知识点先修知识相关知识
-
+ + + + + + + + + +
知识点先修知识相关知识
+