How to replace Pass or Fail value with Green Check Mark or Red X?

How to replace Pass or Fail value with Green Check Mark or Red X?

ColdFlowColdFlow Posts: 6Questions: 3Answers: 0

As the question has stated above, I am trying to write a code that will change all the outcomes of Pass to a Green Check Mark and all Fail outcomes to a Red X?

Below is my code:

columns: [
                { data: "datetime" },
                { data: "forklift" },
                { data: "driver" },
                { data: "shift"},
                { data: "inspection"},
                { data: "status",
                    render: function(data,type){
                        if (type === 'display'){
                            if(data[0] = 'pass'){
                                document.write("✔");
                            } else if (data[0] = 'fail'){
                                document.write("×");
                            }
                            return data;
                        }   
                        return data;
                    }
                },
                { data: "completed"},
            ],

Currently all it shows me is a blank page with nothing but ✔ at the top. How can I fix this where it will display inside my data table?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954
    edited January 2023 Answer ✓

    You probably don't want to use document.write("✔"); as that I think is overwriting your page.

    Take a look at this example of data rendering with columns.render. Instead of document.write("✔"); use return "✔";.

    Also the data for the column is your status object. If its an array then data[0] will work if you are interested in only the first array element. Otherwise, in your if statements use data to to check the value of status.

    Kevin

  • ColdFlowColdFlow Posts: 6Questions: 3Answers: 0
    edited January 2023

    Thanks that actually fixed my issue, now I just need to figure out how to change the color of them. @kthorngren

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954

    You can use rowCallback if the data can change or createdRow if the data is static, like this example.

    Also the data for the column is your status object. If its an array then data[0] will work if you are interested in only the first array element. Otherwise, in your if statements use data to to check the value of status.

    Kevin

  • ColdFlowColdFlow Posts: 6Questions: 3Answers: 0

    @kthorngren Okay, now this issue I am having is that if it is fail it still has a check mark beside it. Any thing I can do to fix that?

  • kthorngrenkthorngren Posts: 21,342Questions: 26Answers: 4,954
    Answer ✓

    I noticed another problem:

    if(data[0] = 'pass'){
    

    In addition to my comment about data[0] you have =. This is an assignment operator not a comparison. Use either == or ===. See this doc for details.

    In order to help we will need to see an example of your data. Please provide a link to your page or build a simple test case showing the issues so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • ColdFlowColdFlow Posts: 6Questions: 3Answers: 0

    @kthorngren I used === and that fixed the issue.

Sign In or Register to comment.