Datatables highlighting does not work with Flask?
Datatables highlighting does not work with Flask?
hedgy123
Posts: 5Questions: 2Answers: 0
Hello,
I have a Flask app that reads in a .csv file and displays it as a datatable. I want to highlight the values in the datatable if they are below a certain threshold. Here's what I did:
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Query</th>
<th>Fraction ok</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Query</th>
<th>Fraction ok</th>
</tr>
</tfoot>
</table>
<script>
function setupData() {
$(document).ready(function () {
$('#example').DataTable({
"aaSorting":[],
"ajax": {
"url":"/get_query_data",
"dataSrc":"data",
"dataType":"json",
"contentType":"application/json"
},
"columns": [
{"data": "Query"},
{"data": "Fraction ok"}
],
"createdRow": function ( row, data, index ) {
if (data[2]*1 < 0.8 ) {
$('td', row).eq(2).addClass('highlight');
}
}
});
});
}
$( window ).on( "load", setupData );
</script>
But the table comes out without any highlighting, even though a whole bunch of values are below my threshold of 0.8. What am I doing wrong?
Thanks so much!
This discussion has been closed.
Answers
Since you are using
columns.data
your data is stored in an object (aka dictionary in Python) but you are trying to access it as an arraydata[2]
. You would usedata["Fraction ok"]
instead. Note that if your data was an array you would usedata[1]
as it is zero based indexing.Also
$('td', row).eq(2).addClass('highlight');
should be$('td', row).addClass('highlight');
. You don't want the.eq(2)
to highlight the row.Kevin
Thanks @kthorngren, but I am afraid it still didn't work. I did:
and the data table still comes out with no highlighting...
Do you get any errors in the browser's console?
Without seeing the data its hard to say whet the problem is. I would add some debug output using console.log, for example:
If you see the output
if is true
then the if statement is working but yourhighlight
class isn't.If this doesn't help then please post a test case replicating the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
EDIT: Just noticed your selector of
$('td', row)
doesn't look right. Checkout thecreatedRow
example here.Kevin