python烧瓶应用中的下拉选项过滤(带JavaScript)
我正在尝试使用下拉词来过滤表中的结果。我试图使其与AddEventListener('click')一起使用,但是我一直坚持如何使其功能正常。我认为我很近,但缺少一些东西。
谁能提供帮助提供一个解决方案,以便当在下拉列表中单击国家价值时,它会过滤到表中所有具有相应国家价值的人?这将需要工作,以便将使用多个列上的多个下拉列表。
python
app = Flask(__name__)
test_data = [['jack', 23, 'china', 'https://www.google.com'],
['jill', 22, 'canada', 'https://www.cnn.com'],
['john', 24, 'canada', 'https://www.cnn.com'],
['jane', 30, 'australia', 'https://www.bbc.com']]
test_df = pd.DataFrame(test_data, columns=['name', 'age', 'country', 'link'])
test_df = test_df.to_dict(orient='index')
@app.route("/hello")
def index():
return render_template("index.html", test_df=test_df)
html :在index.html
<div class="container">
<label for="country">Countries</label>
<form class="form">
<div class="form__group">
<select id="country" name="country" data-dropdown>
<option value="">Please select a country</option>
<option value="australia">Australia</option>
<option value="canada">Canada</option>
<option value="china">China</option>
</select>
</div>
</form>
</div>
<table class="table">
<tbody id="myTable">
</tbody>
</table>
javascript 中:在脚本标签中的index.html中
var mydata = JSON.parse('{{ test_df|tojson }}');
var countrySelector = document.getElementById('country');
// but what next?
buildTable(mydata)
function buildTable(data) {
var table = document.getElementById('myTable')
table.innerHTML = ''
for (var key in data) {
var row = `<tr>
<td>${data[key].name}</td>
<td>${data[key].age}</td>
<td><a href="${data[key].link}" target='_blank'>${data[key].country}</a></td>
</tr>`
table.innerHTML += row
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用select元素的
以下示例向您展示了如何按国家过滤行。为了使在JavaScript中过滤行更容易,它们不是作为DICT传递的,而是使用
df.values.tolist()
作为列表。Use the select element's change event to monitor user changes in the selection.
The following example shows you how to filter the rows by country. To make it easier to filter the rows in JavaScript, they are not passed as a dict but as a list using
df.values.tolist()
.