[英]How to auto refresh html table fields rendered with a for loop in flask?
Flask 中 function 的片段。 我正在生成所有职位的列表,然后将它们发送过来。 然后使用 jinja 和 for 循环填充表格。
@app.route("/")
def table_list():
position_list=["a","b","c","d"]
return render_template("index.html",
position=position_list)
这是在主页上,需要每隔几秒自动刷新一次,因此 position 值将得到更新。
HTML 表:
<table>
<tbody id="data">
{% for pos1, pos2, pos3, pos4 in position %}
<tr id="data_table">
<td id="pos1"> {{ pos1 }} </td>
<td id="pos2"> {{ pos2 }} </td>
<td id="pos3"> {{ pos3 }} </td>
<td id="pos4"> {{ pos4 }} </td>
{% endfor %}
</tbody>
</table>
如何使用 JS仅刷新表中的值,并每隔几秒钟自动刷新一次?
您将不得不更改多项内容才能完成这项工作。
@app.route("/table_list")
def table_list():
return render_template("index.html")
@app.route("/get_list")
def get_list():
position_list = ["a", "b", "c", "d"]
return make_response({"output": position_list})
<table>
<tbody id="data">
<tr id="data_table">
</tr>
</tbody>
</table>
</body>
<script>
setInterval(function () {
let myRequest = new Request('/get_list');
fetch(myRequest).then(response => response.json()).then(function (data) {
let data_table = document.getElementById("data_table");
data_table.innerHTML = "";
for (let d in data['output']) {
data_table.innerHTML += `<td>${data['output'][d]}</td>`
}
});
}, 1000);
</script>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.