制作了一个表格,但未应用“数据表格”。 谢谢。
蟒(3.5)
HTML
<select id="name" class="selectpicker" data-width="100px" title="시도">
<option value='Tiger Nixon'>Tiger Nixon</option>
<option value='Garrett Winters'>Garrett Winters</option>
<option value='Ashton Cox'>Ashton Cox</option>
<option value='Cedric Kelly'>Cedric Kelly</option>
<option value='Airi Satou'>Airi Satou</option>
<option value='Brielle Williamson'>Brielle Williamson</option>
</select>
<button id = "start" class="btn btn-primary " >View</button>
<div id="result"></div>
HTML
脚本 代码向烧瓶发送值
<script>
$(document).ready(function() {
$("#start").click(function(e) {
e.preventDefault();
var selected_name= $("#name").val();
$.post("/result" ,{
name: selected_name
}, function(resp) {
$("#result").html(resp);
});
});
});
</script>
脚本 DatTables脚本
<script>
$(document).ready(function() {
$('#example').DataTable();
} );
</script>
烧瓶应用
@app.route("/")
def index() :
return render_template("table.html")
@app.route("/result", methods=["POST"])
def result():
data = [
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ],
[ "Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000" ],
[ "Cedric Kelly", "Senior Javascript Developer", "Edinburgh", "6224", "2012/03/29", "$433,060" ],
[ "Airi Satou", "Accountant", "Tokyo", "5407", "2008/11/28", "$162,700" ],
[ "Brielle Williamson", "Integration Specialist", "New York", "4804", "2012/12/02", "$372,000" ]
]
df = DataFrame(data,columns = ['name','Position','Office','Extn.','Start date','Salary'])
name = request.form.get("name")
table = df.query('name == "{0}"'.format(name))
return table.to_html(classes='display" id ="example" width="100%')
答案 0 :(得分:0)
这是因为您没有使用dataTable创建表。您正在使用dataTable不知道的jquery追加表的内容。
DataTable提供inbuilt option来进行ajax调用。您必须按如下方式更改代码
$(document).ready(function() {
$('#example').DataTable(
"ajax": {
"url": "/result",
"type": "POST",
"data" : {
name : selected_name
}
},
);
});
在 html 中,你必须创建表元素,以便dataTable能够找到具有给定id或类的表,并将数据附加到该表。
<div id="result">
<table id="example">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn.</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
</table>
</div>
在烧瓶应用中,您无需更改内容为html格式。
@app.route("/")
def index() :
return render_template("table.html")
@app.route("/result", methods=["POST"])
def result():
data = [
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ],
[ "Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000" ],
[ "Cedric Kelly", "Senior Javascript Developer", "Edinburgh", "6224", "2012/03/29", "$433,060" ],
[ "Airi Satou", "Accountant", "Tokyo", "5407", "2008/11/28", "$162,700" ],
[ "Brielle Williamson", "Integration Specialist", "New York", "4804", "2012/12/02", "$372,000" ]
]
# return the results as json # import json
return json.dumps(data)
除此之外,您还可以采取其他方式。在#result
div中附加响应之后,在初始化dataTable之后。
$(document).ready(function() {
$("#start").click(function(e) {
e.preventDefault();
var selected_name= $("#name").val();
$.post("/result" ,{ name: selected_name}, function(resp) {
$("#result").html(resp);
// now initialize dataTable
$('#example').DataTable({
"columns": [
{ "data": "name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "age" },
{ "data": "start_date" },
{ "data": "salary" }
]
});
});
});
});