阿桂天山的技术小结:Flask实现对Ztree树状节点的增改删操作

话不多说,上图上源码

1.实现效果如下图:

阿桂天山的技术小结:Flask实现对Ztree树状节点的增改删操作_第1张图片

阿桂天山的技术小结:Flask实现对Ztree树状节点的增改删操作_第2张图片

阿桂天山的技术小结:Flask实现对Ztree树状节点的增改删操作_第3张图片

阿桂天山的技术小结:Flask实现对Ztree树状节点的增改删操作_第4张图片

2.前端代码index.html





    Ewangda 阿桂天山Ztree实战
    
    
    
    
    
    



    

Flask对Ztree节点的增加,修改与删除

    3.后端代码tree.py

    #=============================================================
    #---------------------阿桂天山 Ewangda--------------------------
    """
        阿桂天山Ztree实战,对树节点的增加,修改与删除
    """
    import op_sql
    import json
    
    from flask import Flask, render_template, request
    
    app = Flask(__name__)
    
    # Ztree管理
    @app.route('/background')
    def tree():
        return render_template("index.html")
    
    
    # 返回所有数据
    @app.route('/_get_tree')
    def get_tree():
        return json.dumps(op_sql.transport())
    
    
    # 增加节点的函数
    @app.route('/_add_tree', methods=['GET', 'POST', 'DELETE'])
    def add_node():
        if request.method == 'POST':
            parent_id = request.form['pId']
            ename = request.form['name']
            op_sql.insert('test-pub', 't_ztree', ename, parent_id)
            return json.dumps({'pId': parent_id, 'name': ename})
    
    
    # 删除结点的函数
    @app.route('/_delete_tree', methods=['DELETE'])
    def delete_node():
        if request.method == 'DELETE':
            eid = request.form['id']
    
            op_sql.delete('test-pub', 't_ztree', eid)
    
        return ''
    
    
    # 更新节点名字的函数
    @app.route('/_update_tree', methods=['POST'])
    def update_node():
        if request.method == 'POST':
            eid = request.form['id']
            newname = request.form['newname']
            op_sql.update("test-pub", "t_ztree", eid, newname)
        return ''
    
    
    # 启动主页面
    if __name__ == "__main__":
        app.run(debug=True)

    3.对数据库的操作op_sql.py

    def insert(database, table, ename, pId):
        conn = pymysql.connect(user="aaa", password="123xxxx", host="127.0.0.1", database=database)
    
        cursor = conn.cursor()
        # 写入语句
        sql_insert = 'insert into %s(name,pId)values("%s","%s");' % (table, ename, pId)
        try:
            cursor.execute(sql_insert)
            conn.commit()
    
        # except Exception as e:
        #     db.rollback()
    
        finally:
            conn.close()
    
    
    def update(database, table, eid, newname):
        conn = pymysql.connect(user="aaa", password="123xxxx", host="127.0.0.1", database=database)
    
        cursor = conn.cursor()
        # 更新语句
        sql_update = 'update %s set name = "%s" where id = "%s";' % (table, newname, eid)
        try:
            cursor.execute(sql_update)
            conn.commit()
        finally:
            conn.close()
    
    
    def delete(database, table, id):
        conn = pymysql.connect(user="aaa", password="123xxxx", host="127.0.0.1", database=database)
    
        cursor = conn.cursor()
        # 删除语句
        sql_delete = 'delete from %s where id =%s;' % (table, id)
        try:
            cursor.execute(sql_delete)
            conn.commit()
        finally:
            conn.close()

    最后,有关数据表结构及测试数据,Ztree的树节点显示,搜索查询等请参考我的另一篇博文

    阿桂天山的技术小结:Flask对Ztree树节点搜索定位icon-default.png?t=N7T8https://blog.csdn.net/gui818/article/details/133696956

    你可能感兴趣的:(python,flask,flask,python)