简体   繁体   中英

Highlight a datatable row if its less than current year

Currently, working on datatables but I need to highlight a row if its less than the current year for example < 2021 or the next year < 2022. I was able to make it work inputting the exact year inside the code but I need it to be dynamic. Like if its already 2022 I want it to be "< 2022" so on and so forth.

Here's my code:

"rowCallback": function(row, data, dataIndex ) {
        var y = new Date();
        if(data["or_number"] == "0"){
            $('td', row).css('background-color', 'red');
        }
        if(data["year"] < "2021"){
            $('td', row).css('background-color', 'red');
        }
    },

Output:

年 < 2021

Maybe

"rowCallback": function(row, data, dataIndex ) {
        var yearNow = new Date().getFullYear();

        console.log("This year is: " + yearNow);

        if(data["or_number"] == "0"){
            $('td', row).css('background-color', 'red');
        }
        if(parseInt(data["year"]) < yearNow){
            $('td', row).css('background-color', 'red');
        }
    },

Cast data["year"] to an int and then check using Date().

"rowCallback": function(row, data, dataIndex ) {
        var y = new Date();
        if(data["or_number"] == "0"){
            $('td', row).css('background-color', 'red');
        }
        if(Number(data["year"]) < y.getFullYear()){
            $('td', row).css('background-color', 'red');
        }
    },

Thanks for all the help. :)

Here's the final touch. This is now working.

"rowCallback": function(row, data, dataIndex ) {
        var y = new Date().getFullYear();
        if(data["or_number"] == "0"){
            $('td', row).css('background-color', 'red');
        }
        if(parseInt(data["year"]) < y ){
            $('td', row).css('background-color', 'red');
        }
    },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM