我正在构建一个Flask应用程序,其中包括一个表,用户可以在其中更改单元格并“应用”这些更改,例如在数据库中更新。在
我在让flask检索通过表单提交的数据时遇到问题。在
行数是动态的,取决于列表中Id的数量。在
这是我的html——很抱歉,我还在学习。在
<tbody>
<form method="post" name="productCost">
{% for n in range(name_list | length) %}
<tr>
<th scope="row" >{{name_list[n]}}</th>
<td>{{ID_list[n]}}</td>
<td id="cost{{n}}">${{cost_list[n]}}</td>
<td>---</td>
<td id="changes{{n}}"><button onclick="costChanges{{n}}()">Edit</button></td>
</tr>
<script>
function costChanges{{n}}() {
document.getElementById("cost{{n}}").innerHTML = "<input placeholder='{{cost_list[n]}}' name={{ID_list[n]}}>";
document.getElementById("changes{{n}}").innerHTML = "<button onclick='applyChanges{{n}}()' type='submit'>Apply</button><button onclick='cancelChanges{{n}}()'>Cancel</button>";
}
function applyChanges{{n}}() {
docuemnt.getElementById("cost{{n}}").innerHTML = document.forms["productCost"]["{{ID_list[n]}}"]
}
function cancelChanges{{n}}() {
document.getElementById("cost{{n}}").innerHTML = "{{cost_list[n]}}";
document.getElementById("changes{{n}}").innerHTML = "<button onclick='costChanges{{n}}()'>Edit</button>";
}
</script>
{%endfor%}
</form>
</tbody>
这是我的python/flask代码:
^{pr2}$我需要python识别所做的更改并将其存储在变量中。这是为了在数据库中更新它——但我不需要数据库代码方面的帮助。我只需要帮助python获取已更改的单元格并识别它所在的行。在
将
jquery
与ajax
一起使用要简单一些。后者使您能够通过与后端脚本通信来动态更新表以更新数据库。在首先,创建HTML脚本(下面的代码为本例创建了一个不同的表布局,但是,您可以根据下面的代码替换自己的表布局)。jquery干净地处理Apply、Cancel和Edit按钮功能,并通过
ajax
与后端通信:tables.html
:然后,创建应用程序和路由:
^{pr2}$现在,在
'/update_cell'
中,您有了新的price值和product row id,它告诉您要更新哪个sqlite行的价格。注意,在home
中,enumerate
获得的行id对应用程序至关重要,因为它使jquery
知道要更新什么表和sqlite行。在演示:
相关问题 更多 >
编程相关推荐