3

I have array values like this. I want to display these values in HTML table tag

<script type="text/javascript">
var orderArray = [
    ["1","29-Aug-2012", "Product1", "client1"],
    ["2","29-Aug-2012", "Product2", "client2"],
    ["3","29-Aug-2012", "Product3", "client3"],
    ["4","29-Aug-2012", "Product4", "client4"],
    ["5","29-Aug-2012", "Product5", "client5"]
    ];

function display()
{
    for(i=0;i<ordertArray.length;i++)
    {
    //How to display values of array inside the div or table tag ???
    }
}

</script>


How to display values of array inside the div or table tag ???

Maayi
  • 157
  • 2
  • 2
  • 11

4 Answers4

6

orderArray's items represent <tr> elements, and each item inside represents a <td> element. So you can loop through orderArray creating <tr>s, and then loop through its elements on each loop creating <td>s: http://jsfiddle.net/h7F7e/.

var table = document.getElementById("table");  // set this to your table

var tbody = document.createElement("tbody");
table.appendChild(tbody);
orderArray.forEach(function(items) {
  var row = document.createElement("tr");
  items.forEach(function(item) {
    var cell = document.createElement("td");
    cell.textContent = item;
    row.appendChild(cell);
  });
  tbody.appendChild(row);
});
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • How to display this table inside a div on particular location ? – Maayi Aug 29 '12 at 13:19
  • @Ismail Vitla: I actually assumed you already have a table. Just define the table in HTML somewhere and set `var table = ...` so that it refers to the table. – pimvdb Aug 29 '12 at 13:20
  • 1
    [Doesn't work for IE 8 or lower](http://stackoverflow.com/questions/812693/cant-dynamically-add-rows-to-a-table-in-ie) – Shadow The GPT Wizard Aug 29 '12 at 13:21
2

Something like this would create a dynamic table for you :

// get handle on div
var container = document.getElementById('container');
// create table element
var table = document.createElement('table');
var tbody = document.createElement('tbody');
// loop array
for (i = 0; i < orderArray.length; i++) {
    // get inner array
    var vals = orderArray[i];
    // create tr element
    var row = document.createElement('tr');
    // loop inner array
    for (var b = 0; b < vals.length; b++) {
        // create td element
        var cell = document.createElement('td');
        // set text
        cell.textContent = vals[b];
        // append td to tr
        row.appendChild(cell);
    }
    //append tr to tbody
    tbody.appendChild(row);
}
// append tbody to table
table.appendChild(tbody);
// append table to container
container.appendChild(table);

Uses document.createElement() and element.appendChild()

Working example here

Manse
  • 37,765
  • 10
  • 83
  • 108
2

Use the DOM functions of table elements:

function display() {
    var table = document.createElement("table");
    for (var i=0; i<orderArray.length; i++) {
        var row = table.insertRow();
        for (var j=0; j<orderArray[i].length; j++) {
            var cell = row.insertCell();
            cell.appendChild(document.createTextNode(orderArray[i][j]));
        }
    }
    return table;
}

Call that function when the DOM is ready, and append the returned table somewhere.

Manse
  • 37,765
  • 10
  • 83
  • 108
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Works perfectly but one of my `