1

enter image description here

I am facing an issue in javascript, I want to display a column Success/Fail in table but they shows two columns in table.. they don't show the correct value in Success/Fail column.

What should i do? Anyone help me?

        var responseList = [{
            "summary": {
                "template_name": "test",
                "success": "2",
                "fail": "1",
            },
            "summary": {
                "template_name": "test",
                "success": "3",
                "fail": "2",
            },

        }];


        var table = document.querySelector('#my-table');
        var tbody = document.createElement('tbody');
        table.appendChild(tbody);


        for (var i = 0; i < responseList.length; i++) {

            var tr = tbody.insertRow();

            var summary = responseList[i]["summary"];
            console.log(summary);
            for (var key in summary) {
                if (summary.hasOwnProperty(key)) {
                    console.log(key + " -> " + summary[key]);
                    var td = tr.insertCell();
                    td.innerHTML = summary[key];
                      if(key == "success" || key == "fail"){
                      // console.log("success",summary[key]);
                      // console.log("fail",summary[key]);
                      var td = tr.insertCell();
                      td.innerHTML = `${summary[key]} Successful / ${summary[key]} Failed`;
                      }

                }
            }
        }
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>

</head>

<body>
    <table id="my-table" border="0">
        <thead>
            <tr>
                <th>Template name</th>
                <th>Success</th>
                <th>Success/Fail</th>
                <th>Fail</th>
                <th>Success/Fail</th>
            </tr>
        </thead>


    </table>
</body>

</html>

6
  • What you want to achieve is not clear in your query. Add desired output instead. Commented Jul 17, 2020 at 5:05
  • 1
    ok i will share expected output @ Commented Jul 17, 2020 at 5:05
  • i add a photo expected output . you can check it @ Commented Jul 17, 2020 at 5:10
  • can you understand desired output? @ Commented Jul 17, 2020 at 5:18
  • desired op headings are not matching with the table headings you have mentioned in query html Commented Jul 17, 2020 at 5:21

1 Answer 1

1

Your attempt is a little complex for what it needs to do, there isn't much value iterating over the keys of the summary object in a loop to build the table. It's much simpler to access the keys directly as i've done below.

var responseList = [
    {
        "template_name": "test",
        "success": "2",
        "fail": "1",
    },
    {
        "template_name": "test",
        "success": "3",
        "fail": "2",
    },
];

var table = document.querySelector('#my-table');
var tbody = document.createElement('tbody');
table.appendChild(tbody);

responseList.forEach(summary => {
  var tr = tbody.insertRow();
  tr.insertCell().innerHTML = `${summary.template_name}`
  tr.insertCell().innerHTML = `${summary.success}`
  tr.insertCell().innerHTML = `${summary.fail}`
  tr.insertCell().innerHTML = `${summary.success}/${summary.fail}`
})
<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>

</head>

<body>
    <table id="my-table" border="0">
        <thead>
            <tr>
                <th>Template Name</th>
                <th>Success</th>
                <th>Fail</th>                
                <th>Success/Fail</th>
            </tr>
        </thead>


    </table>
</body>

</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.