更新表后烧瓶,自动重新加载添加按钮更新数据到mysql

时间:2021-05-17 17:41:03

标签: python html jquery ajax flask

我想在更新表格时自动重新加载表格。请任何人都可以帮助我,我被困了好几天。

以下是我尝试过的代码:

index.html

    $(document).on("click", ".update", function(){
    var id = $(this).attr("id");
    var string = id;
    var txtid = $("#txtid").val();
    var txtname = $("#txtname").val();
    var txtdatetime = $("#txtdatetime").val();
    v
    req=$.ajax({
        url : '/ajax_update',
        type : 'POST',
        data :  { string: string, txtid: txtid,txtname: txtname,txtdatetime: txtdatetime, }

        });
        
    req.done(function(data) {

        //$('#sample_data1').fadeOut(1000).fadeIn(1000);
        $('#sample_data1').html(data);
        $('#sample_data1').show();

     });
    
    
 });
// Edit row on edit button click
$(document).on("click", ".edit", function(){  
    $(this).parents("tr").find("td:not(:last-child)").each(function(i){
        if (i=='0'){
            var idname = 'txtid';
            $(this).html('<input type="text" name="updaterec" id="' + idname + '" class="form-control" value="' + $(this).text() + '">');
        var idname = 'txtname';
            $(this).html('<input type="text" name="updaterec" id="' + idname + '" class="form-control" value="' + $(this).text() + '">');
        }else if (i=='2'){
            var idname = 'txtdatetime';
            $(this).html('<input type="date" name="updaterec" id="' + idname + '" class="form-control" value="' + $(this).text() + '">');
        }
    $(this).parents("tr").find(".add, .edit").toggle();
    $(".add-new").attr("disabled", "disabled");
    $(this).parents("tr").find(".add").removeClass("add").addClass("update"); 
});

app.py

@app.route("/ajax_update",methods=["POST","GET"])
def ajax_update():
cursor = mysql.connection.cursor()
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
if request.method == 'POST':
    string = request.form['string']
    txtname = request.form['txtname']
    txtdatetime = request.form['txtdatetime']
   cur.execute("UPDATE tool SET name = %s, date_time = %s, string])
    
    mysql.connection.commit()   
        
    cur.close()
    msg = 'Record successfully Updated'   
    #return jsonify({'result' : 'success'})  
return render_template('section.html')  
#return json.dumps({'status':'OK'})

section.html

<tbody id="sample_data1">
                {% for row in emp %}    
                <tr>
                    <td>{{row.id}}</td>
                    <td> <a class="get" href="#">{{row.name}}</a></td>
                    <td>{{row.date_time}}</td>
                  </tr>
                {%end for%}
</tbody>

当我点击更新按钮时,我希望页面自动重新加载新数据。如果我在这里遗漏了什么,请告诉我。感谢您的帮助

0 个答案:

没有答案
相关问题