If and else condition with Data Tables

If and else condition with Data Tables

pcolynpcolyn Posts: 8Questions: 2Answers: 0

hello,

I have some knowledge in html, php, css but still a novice in Javascript.

I already do a lot of advancement with my table. I read a lot all the forum and the guide on this site to build my own data table and it was very useful but I'm still blocked but using If and else conditions.

could someone help me?

here is a part of the code:

columns: [

                { data: "data1" },
                { data: "data2" },
                { data: "nom" },
                { data: "prenom" },
                { data: "data3" },
                { data: "data4" },
                {data: "imageUrl",
                    "searchable": false,
                    "orderable":false,
                    "render": function (data, type, row) {

                    if ({data:"nom"} === 'henri') {
                        return '<img src="image/valid.png" style="width:15px" />';}

                        else {

                return '<img src="image/error.png" style="width:15px" />';

            }
                    }

                }

            ]

the problem is on my If and else condition:

what I want is:

when the data:"nom" has on value: "henri" is showing the picture "valid.png"
otherwise it is showing the picture "error.png"

problem:
the picture error.png is always displayed also for the data:"nom" with the value "henri"

could you please help me to find the right syntax.

thanks in advance for your help

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin
    Answer ✓

    if ({data:"nom"} === 'henri') {

    That is checking to see if the object {data:"nom"} is the same as the string henri - which it isn't.

    You want:

    if ( row.nom === 'henri') {
    

    Allan

  • pcolynpcolyn Posts: 8Questions: 2Answers: 0

    really thanks, it is working
    I was sure that it was a syntax error, but I'm learning with this solution

    thanks so much I can continue :)

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    Answer ✓

    Use row.nom or row["nom"] instead of {data:"nom"}. data is just the imageUrl - the cell data, for the full row data use the row variable with the syntax in my first sentence.

This discussion has been closed.