Flask, json, render_template, form action, data tables
It will be best to have a second route that returns json, and utilise the ajax data source of datatables. A basic outline:
Your json route (dropdown as a request arg):
@app.route('/get_data')def get_json(): filter_val = request.args.get('filter') result = # Your query here (using filter_val, or all rows if none?) return jsonify(result)
Your current route will likely remain as-is, but:
def my_page(): # ... return render_template('page.html')
And the js:
$('#table').DataTable( { ajax: { url: "{{ url_for('get_json') }}?filter=" + $("#dropdown").val(), dataSrc: '' }, columns: [ ... ]} );
This is explained in a lot more (better) detail on the datatables ajax guide. But I hope this is a start.
Edit:
One this is setup, you can handle the re-fetching of data slightly differently. You no longer need the form. You can handle either the click of a button, or or change of the dropdown (below). Calling the table.reload()
function will refetch the data. Something like:
$("#the_dropdown").change(function() { table.reload();});