这是我第一次构建网络应用程序,我想从头开始学习基础知识。因此,我有一个Python脚本,我想在终端上打印该数据表时进行练习,并决定使用Flask,HTML,CSS,JS在网页上显示该表。到目前为止,我已经做到了。
现在,我想通过单击表标题对该表进行排序。
这是我的代码的总体分解以及到目前为止我所做的事情:
Python脚本
from flask import Flask, render_template, request, url_for
app = Flask(__name__)
def get_table():
...
return dict //returns list of dictionaries, for example...
//dict = [{'name':'Joe','age':'25'},
// {'name':'Mike','age':'20'},
// {'name':'Chris','age':'29'}]
@app.route("/")
def home():
return render_template('home.html')
@app.route("/table", methods = ['POST','GET'])
def table():
if request.method == 'GET': //When button pressed on homepage
dict_table = get_table() //return list of dictionaries of names and ages
return render_template('table.html', dict_table=dict_table)
if __name__ == '__main__':
app.run(debug=True)
HTML文件table.html
<!DOCTYPE html>
<html>
<head>
<title>Display Table</title>
<script type="text/javascript" src="/scripts/app.js"></script>
</head>
<body>
<table id="table">
{% if dict_table %}
<tr>
{% for key in dict_table[0] %}
<th onclick="sortTable(1)" style="cursor:crosshair">{{ key }}</th>
{% endfor %}
</tr>
{% endif %}
{% for dict in dict_table %}
<tr>
{% for value in dict.values() %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
</html>
因此该表可以在我的Web浏览器上正确显示。我在app.js脚本中使用了他们在W3学校网站上教的排序功能。
app.js
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("myTable");
switching = true;
//Set the sorting direction to ascending:
dir = "asc";
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for (i = 1; i < (rows.length - 1); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
/*check if the two rows should switch place,
based on the direction, asc or desc:*/
if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch= true;
break;
}
} else if (dir == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
//Each time a switch is done, increase this count by 1:
switchcount ++;
} else {
/*If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again.*/
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
}
这是我从https://www.w3schools.com/howto/howto_js_sort_table.asp那里获得的示例
到目前为止,这是我的问题
如果有人可以指导我完成这项工作,我会很乐意。我也愿意听听是否还有更好的方法可以做到这一点,只要它不是太复杂并且涉及安装外部应用程序(例如一些我已经看到过类似问题的解决方案)(因为我只是初学者并尝试学习)。谢谢!
答案 0 :(得分:1)
n
函数中的参数sortTable
是行号。并且,在呈现表头时,您始终会传递1。要解决该问题,您可以尝试
{% for key in dict_table[0] %}
<th onclick="sortTable({{ loop.index0 }})" style="cursor:crosshair">{{ key }}</th>
{% endfor %}
应该可以解决问题。
有关更多信息,请访问此jinja tricks page和完整的jinja documentation。
UPD 。还有control structures的列表。