Compare int values and color if higher or lower
Compare int values and color if higher or lower
SanZamoyski
Posts: 12Questions: 2Answers: 0
Hi!
I have table consists of datetime column and 16 values.
I generaly sort table by datetime so newwest values are on top.
I would like to compare first row with second, second with third and so on.
If first row value is higher - mark it green if lower - red. Same for all columns with values.
Can it be done with datatables?
Best regards!
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Use
columns.render
to do this.Allan
Hi!
Thanks for quick response. I'm trying something like this:
code snippet on this forum is a pain...
definied columns:
and function:
function compare_cells(data, type, full, meta){
but color does not change (checked - comparation goes well).
I'm really lost...
please help.
Can you link to a page showing the issue so I can debug it live please.
Allan
Sent to You by PM.
In the
compare_cells
function thenext_data
value appears to always be undefined.This is because you are trying to get the value from the next row in the table - but as each row is added, the next row doesn't yet exist! Hence the
undefined
value. The rows have to be added sequentially.So you need to have all of the rows available before you can perform the calculation you want.
On the plus side, once they are there, all you need to do is use
rows().invalidate()
:You could add that call into
initComplete
.Allan
Thats incorrect!
"render": function ( data, type, full, meta ) {
var t = compare_cells(data, type, full, meta);
console.log(t);
return t;
}
gives console log:
...
<p style="color:red">red 065</p>
interfejs.php:56 <p style="color:green">green 062</p>
interfejs.php:56 <p style="color:red">red 208</p>
interfejs.php:56 <p style="color:red">red 073</p>
interfejs.php:56 <p style="color:red">red 063</p>
interfejs.php:56 <p style="color:red">red 059</p>
interfejs.php:56 <p style="color:red">red 058</p>
...
I've just tried loading the page again but the server is timing out.
When I used Chrome's trace features the
next_data
value wasundefined
as I stated. Unfortunately I can't try it again with the server offline.It's up now!
My dump router changed ip of the 'server', so forwarding stoped to work.
Fortunetly there was somenone at university who could change this.
Thanks for getting the page back online. While you are correct, sometimes the rendered value does return the required HTML, that is not the rendered value that DataTables displays. DataTables will call the render function for a number of things, including the display data, sort data, filter data etc.
My explanation above is still correct and valid.
Did you try running
$('#data').DataTable().rows().invalidate()
as I suggested above? That would show you the data you want to be rendered (until you reload the data which it appears to be doing periodically).The issue, as I stated, is that you are trying to check a future row, which has not been added at the point you need it to be. If you can reverse the logic so it will check the previous row, that would work.
Allan
Ok, now i get it!
I did't try it, since i assumed i do not need dinamical colouring. I've just made in on server side - php generates ajax data with styled span. I know it is not gentle way, but it is fair enought at the moment for me.
Thanks!