Skip to content Skip to sidebar Skip to footer

Add Table Header For Dynamic Table In Javascript

I'm trying to create a table in javascript and put a header on it. I tried to incorporate the answer from this SO question but perhaps I didn't include it in the right place. The b

Solution 1:

Solution1 The way you are appending tbody rows, you can insert the heading as well. So instead of tbl.append(header) and defining the header string, you can use something like below:

results = {
  weak_sent: [
    "row 1 data",
    "row 2 data"
  ],
  weak_sent_num: [1,2]
}
functiongenerate_table() {
    // get the reference for the bodyvar body = document.getElementsByTagName("body")[0];

    // creates a <table> element and a <tbody> elementvar tbl = document.createElement("table");
    //var header = document.createElement("header");// var header = '<tr><th>Country</th><th>City</th></tr>';var header=  document.createElement('thead')
    var headingRow = document.createElement('tr')

    var headingCell1 = document.createElement('td')
    var headingText1 = document.createTextNode('country')
    headingCell1.appendChild(headingText1)
    headingRow.appendChild(headingCell1)
    
    var headingCell2 = document.createElement('td')
    var headingText2 = document.createTextNode('City')
    headingCell2.appendChild(headingText2)
    headingRow.appendChild(headingCell2)

    header.appendChild(headingRow)
    tbl.appendChild(header)
    //var header = "<th>Header</th>";var tblBody = document.createElement("tbody");


    // creating all cellsfor (var i = 0; i < results.weak_sent.length; i++) {
        // creates a table rowvar row = document.createElement("tr");

        for (var j = 0; j < 2; j++) {
            // Create a <td> element and a text node, make the text// node the contents of the <td>, and put the <td> at// the end of the table rowvar cell = document.createElement("td");
            if (j == 0) {
                var cellText = document.createTextNode(results.weak_sent_num[i]);
            } else {
                var cellText = document.createTextNode(results.weak_sent[i]);
            }


            cell.appendChild(cellText);
            row.appendChild(cell);
        }

        // add the row to the end of the table body
        tblBody.appendChild(row);
    }
    // This is for the quick solution// tbl.innerHTML = header// put the <tbody> in the <table>
    tbl.appendChild(tblBody);



    // appends <table> into <body>
    body.appendChild(tbl);
    // sets the border attribute of tbl to 2;
    tbl.setAttribute("border", "2");
}

generate_table()

Solution2 As a quick solution you can use innerHTML property, as shown below.

results = {
  weak_sent: [
    "row 1 data",
    "row 2 data"
  ],
  weak_sent_num: [1,2]
}
functiongenerate_table() {
    // get the reference for the bodyvar body = document.getElementsByTagName("body")[0];

    // creates a <table> element and a <tbody> elementvar tbl = document.createElement("table");
    //var header = document.createElement("header");var header = '<tr><th>Country</th><th>City</th></tr>';

    //var header = "<th>Header</th>";var tblBody = document.createElement("tbody");


    // creating all cellsfor (var i = 0; i < results.weak_sent.length; i++) {
        // creates a table rowvar row = document.createElement("tr");

        for (var j = 0; j < 2; j++) {
            // Create a <td> element and a text node, make the text// node the contents of the <td>, and put the <td> at// the end of the table rowvar cell = document.createElement("td");
            if (j == 0) {
                var cellText = document.createTextNode(results.weak_sent_num[i]);
            } else {
                var cellText = document.createTextNode(results.weak_sent[i]);
            }


            cell.appendChild(cellText);
            row.appendChild(cell);
        }

        // add the row to the end of the table body
        tblBody.appendChild(row);
    }
    // This is for the quick solution
    tbl.innerHTML = header
    // put the <tbody> in the <table>
    tbl.appendChild(tblBody);



    // appends <table> into <body>
    body.appendChild(tbl);
    // sets the border attribute of tbl to 2;
    tbl.setAttribute("border", "2");
}

generate_table()

Post a Comment for "Add Table Header For Dynamic Table In Javascript"