How to highlight a row with a date?

How to highlight a row with a date?

swimerswimer 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

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770
    edited June 2022 Answer ✓

    Actually createdRow and createdCell 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:

    if ( new Date( data ) < d ) {
    

    Otherwise I suspect you are comparing a string (data) to Date 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

  • swimerswimer Posts: 3Questions: 1Answers: 0

    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.

  • swimerswimer Posts: 3Questions: 1Answers: 0

    I solved my problem.

     "columnDefs": [{
                type: 'natural', targets: 1
            },
            {
                targets: 9,
                render: function (data, type, row, meta) {
                    var nowday = new Date();
                    var dateParts = data.split(".");
                    var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
                    if (dateObject.setMonth(dateObject.getMonth() - 1) < nowday){
                        return '<span style="color: red;">' + data + '</span>';
                    } else {
                        return data;
                    }
                }
            }]
    

    Now I need do just sorting for (dd.mm.yyyy) format

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    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

Sign In or Register to comment.