CreatedRow outputting Opposite of Expected Result

CreatedRow outputting Opposite of Expected Result

zgoforthzgoforth Posts: 493Questions: 98Answers: 2
edited August 2021 in DataTables 1.10

Link to test case: https://jsfiddle.net/BeerusDev/940cedtm/8/

I am trying to use the createdRow callback to style the base row, removed rowgroup and everything unneccessary to this application, to have less eye strain on the code. I am trying to apply a styling to the base row based on if the Current Date (Today) is before one month before the retention date, if it isn't then it will style it red, but if it is it should be green.

The date itself is so it should be green, but I am getting a red output.

"createdRow": function( row, data, dataIndex){


            let notiDate = moment(data[5]).subtract(1, 'months').format("MM/DD/YYYY");
            if(moment(data[7]).isBefore(notiDate)){
                $(row).addClass('green');
            } 
            if(moment(data[7]).isAfter(notiDate)){
                $(row).addClass('red');
            }
        },

Here is a different, sort of similar attempt still returning red: https://jsfiddle.net/BeerusDev/940cedtm/14/

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765
    Answer ✓

    I added these two console.log statements to your code:

                console.log(notiDate)
                console.log(moment(data[7]))
    

    This is the output:

    07/31/2021
    ?editor_console=true:208 k {_isAMomentObject: true, _isUTC: false, _pf: {…}, _locale: x, _d: Tue Aug 31 2021 16:42:41 GMT-0400 (Eastern Daylight Time), …}
    

    Seems like the result should be Red as data[7] (08/31/2021) is after notiDate (07/31/2021). I don't understand your requirement but it sounds like you need to change your code logic to match what you want.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    That doesn't make since though if you look at the columns as well as the sample data as they are in the same order. I can't console.log any of the data[i] elements they all return undefined? When I use moment() is getting the current instance, that is why it is returning a todays date, then subtracting the month from it. Why is data[i] undefined, no matter the index? https://jsfiddle.net/BeerusDev/940cedtm/27/

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    But data.Retention date works. I am confused what is the different uses for those two different ways?

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765
    edited September 2021 Answer ✓

    Your data structure is using objects not arrays. So you need to access the data using object notation ie, data.Retention. The moment() API must default to some value if the parameter is undefined. You will need to refer to the moment.js docs for answers to that question.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    Thanks for the clarification Kevin

Sign In or Register to comment.