要使用jQuery Tree插件實(shí)現(xiàn)動態(tài)加載,您需要遵循以下步驟:
<!-- 引入jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- 引入jQuery Tree插件 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-tree/1.0.0/jquery.tree.min.js"></script>
<div id="tree"></div>
$(document).ready(function() {
$("#tree").tree({
url: "your-data-source-url", // 數(shù)據(jù)源URL,這里應(yīng)該是一個返回樹形結(jié)構(gòu)數(shù)據(jù)的API接口
onClick: function(node) {
// 點(diǎn)擊樹節(jié)點(diǎn)時的操作,例如顯示詳細(xì)信息
console.log("Node clicked:", node.text);
}
});
});
from flask import Flask, jsonify, request
app = Flask(__name__)
# 模擬數(shù)據(jù)
data = [
{"id": 1, "parent_id": None, "text": "Node 1"},
{"id": 2, "parent_id": 1, "text": "Node 1.1"},
{"id": 3, "parent_id": 1, "text": "Node 1.2"},
{"id": 4, "parent_id": 2, "text": "Node 1.1.1"},
]
@app.route("/get_tree_data", methods=["GET"])
def get_tree_data():
parent_id = request.args.get("parent_id")
if parent_id is None:
# 獲取所有頂級節(jié)點(diǎn)
nodes = [{"id": node["id"], "parent_id": None, "text": node["text"]} for node in data]
else:
# 獲取指定父節(jié)點(diǎn)的子節(jié)點(diǎn)
parent_node = next((node for node in data if node["id"] == int(parent_id)), None)
if parent_node is None:
return jsonify([])
nodes = [{"id": node["id"], "parent_id": parent_node["id"], "text": node["text"]} for node in data if node["parent_id"] == parent_node["id"]]
return jsonify(nodes)
if __name__ == "__main__":
app.run(debug=True)
現(xiàn)在,當(dāng)您訪問/get_tree_data?parent_id=null
時,將返回包含所有頂級節(jié)點(diǎn)的樹形結(jié)構(gòu)數(shù)據(jù)。您可以根據(jù)需要修改API接口以返回其他級別的節(jié)點(diǎn)數(shù)據(jù)。