Conditionally set column text color to numbers in DataTables
Conditionally set column text color to numbers in DataTables
I have a simple table that has a column of numbers (eg.1.08%). If the number is negative I want the color to be red.(ie.-1.08% entirely in red color) and if the number is positive then the color should be green. I've found examples of how to make the whole ROW red, but I only want to set the color of the text in the cell to red. I'm new to DataTables and I'm guessing this is simple, but I can't figure out how to do it. Note: I have data array in JSON format.
This question has an accepted answers - jump to answer
Answers
loop through all rows and certain cell in each row you change
Hi @cuperak ,
Thanks. But how should I make the text color to red or green?
M sorry, but will you plz explain me this in brief as I am a newbie here! Thanks.
i did this a different way,
set a couple of css classes
then in the table, render the column, adding the class, dependent on the cell value...
Hi @crush123 ,
Thanks for the reply. I have tried this but its not working. Plz check my code:-
var dataSet = [ [ "-1.08", "3.12", "-2.18", "5.25", "8.65", "3.33" ], [ "-1.08", "3.12", "-2.18", "5.25", "8.65", "3.33" ], [ "-1.08", "3.12", "-2.18", "5.25", "8.65", "3.33" ], [ "-1.08", "3.12", "-2.18", "5.25", "8.65", "3.33" ], [ "-1.08", "3.12", "-2.18", "5.25", "8.65", "3.33" ], [ "-1.08", "3.12", "-2.18", "5.25", "8.65", "3.33" ], ]; $(document).ready(function() { $('#example').dataTable({ aaData : dataSet, render: function ( data, type, row ) { if (data >= 0) { return '[code]
<html>
<head>
<title>Datatable</title>
<link rel="stylesheet" type="text/css"
href="//cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" />
<link rel="stylesheet" href="new.css">
<head>
'+data+'
'; } else { return ''+data+'
'; } }, columns : [ { title : "30-day", }, { title : "60-day" }, { title : "90-day" }, { title : "4-months" }, { title : "6-months" }, { title : "1 year" } ] }); });</head>
<body>
<table id="example" class="display" width="100%"></table>
</body>
</html>
P.S. I've included the css code in new.css file.
try this for starters, make sure you include the js libraries..
@crush123
Thanks a lot...that helped!