How to highlight a row with a date?
How to highlight a row with a date?
swimer
Posts: 3Questions: 1Answers: 0
There is a column with dates. It is necessary to highlight some dates with a color when there is a 1 month left before the date.
It seemed that I need to use columnDefs.
I separately found how to calculate the date:
date.setMonth(date.getMonth() - 1);
Description of problem: I don't understand how to apply this when displaying the table.
Please tell me what to do if I'm wrong.
"columnDefs": [{
type: 'natural', targets: 1
},
{
targets: 9,
render: function (data, type, row, meta) {
var d = new Date(data);
d.setMonth(d.getMonth() - 1);
if (data < d ) {
color = 'red';
return '<span style="color:' + color + '">' + data + '</span>';
} else{
return data;
}
}
}]
This question has an accepted answers - jump to answer
Answers
Actually
createdRow
andcreatedCell
are intended to be used for applying styles like this. However the code you posted looks like it should work. But you probably will need to convert the column data to Javascript Date object too. You might need to do something like this:Otherwise I suspect you are comparing a string (
data
) toDate object (
d`).If this doesn't help then please provide a link to your page or a test case replicating the issue so we can help debug.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Sorry for the long reply, got distracted by another project.
You are right, the code is working. Everything works http://live.datatables.net/gokurila/2/watch?html,js,output.
The problem arises because of the date format that I use.
I use format dd.mm.yyyy for date.
I think I need to format the date in Linux format, reduce it by a month, compare and only then display it with a color if the necessary.
I solved my problem.
Now I need do just sorting for (dd.mm.yyyy) format
It would be worth looking at the DateTime example here. This was significantly improved in the 1.12 release, so it would be worth updating to that if you haven't done so already,
Colin