简体   繁体   English

如果小于当前年份,则突出显示数据表行

[英]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.目前,正在处理数据表,但如果它小于当前年份,例如 < 2021 或下一年 < 2022,我需要突出显示一行。我能够使其工作在代码中输入确切的年份,但我需要它动态的。 Like if its already 2022 I want it to be "< 2022" so on and so forth.就像它已经是 2022 年一样,我希望它是“< 2022 年”等等。

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().将 data["year"] 转换为 int,然后使用 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');
        }
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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