columns.render() - possible using if statements?

columns.render() - possible using if statements?

JoeJoeJoeJoeJoeJoe Posts: 50Questions: 13Answers: 1
edited November 2018 in Free community support

There are three different images I would like to display in my column.render() depending on data output. If I am using only two images I can get the code to work using: return (if true) ? display this : else display this. However, I am not able to get the code to work when working with more than two conditions using if statements. This is my faulty code:

"data": "ErrorCount",
"render": function (data, type, row) {
                    if (data === "01/01/9999 00:00:00") {
                        data = '<i class="far fa-dot-circle" style="color:yellow" /*aria-hidden="true"*/></i>';
                    } else if (data === 0) {
                        data = '<i class="far fa-dot-circle" style="color:green" /*aria-hidden="true"*/></i>';
                    } else {
                        data = '<i class="far fa-dot-circle" style="color:red" /*aria-hidden="true"*/></i>';
                    }

What is the correct way of doing this with more than two conditions? Thank you.

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421
    Answer ✓
    {   data: "ErrorCount",
        render: function (data, type, row) {
            if (data === "01/01/9999 00:00:00") {
                return '<i class="far fa-dot-circle" style="color:yellow" /*aria-hidden="true"*/></i>';
            }
            if (data == 0) {
                return '<i class="far fa-dot-circle" style="color:green" /*aria-hidden="true"*/></i>';
            }
            return '<i class="far fa-dot-circle" style="color:red" /*aria-hidden="true"*/></i>';
         }
    },
    
  • JoeJoeJoeJoeJoeJoe Posts: 50Questions: 13Answers: 1
    edited November 2018

    Thank you, seems really obvious now that you mention it

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421

    You are welcome. By the way, I changed the comparison operator from "===" to "==" because as far as I am aware Data Tables returns numbers as strings (if you use Editor on your server). With "==" it should work either way.

  • JoeJoeJoeJoeJoeJoe Posts: 50Questions: 13Answers: 1

    Thanks for the info. I will bear that in mind.

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421

    I use "switch" a lot for rendering Data Table columns. Switch compares in strict mode like "===". This example is about a boolean variable that is saved in a TINYINT field in my MySQL database. Editor returns it as a string. Hence I need to use this in my "switch" for rendering:

    switch (row[0].public) {
        case '1':
            return 'Public';
            break;
        case '0':
            return 'Restricted to:' ;
            break;
    }
    

    Without the ' it doesn't work ...

This discussion has been closed.