puiu91
11/30/2015 - 6:05 PM

Swap table body in Javascript

Swap table body in Javascript

function createTableBody() {
  
  var newTbody = document.createElement('tbody')

  // create and populate new rows by looping through database query results
  for (var i = 0; i < data.length; i++) {

    // insert rows
    var newRow = newTbody.insertRow(i)

    // insert cells
    newRow.insertCell(0).appendChild(document.createTextNode(data[i].id))
    newRow.insertCell(1).appendChild(document.createTextNode(data[i].someOther1))
    newRow.insertCell(2).appendChild(document.createTextNode(data[i].someOther2))
    newRow.insertCell(3).appendChild(document.createTextNode(data[i].someOther3))
    newRow.insertCell(4).appendChild(document.createTextNode(data[i].someOther4))
    newRow.insertCell(5).appendChild(document.createTextNode(data[i].someOther5))

    // create button
    var button = document.createElement('button')
    button.setAttribute('ui-role', 'moreDetails')

    // create button icon
    var buttonIcon = document.createElement('i')
    buttonIcon.setAttribute('class', 'fa fa-folder-open-o')

    // append button icon to button
    button.appendChild(buttonIcon)

    // append item to final row
    newRow.insertCell(6).appendChild(button)
  };
  
  // replace existing placeholder tbody with the populated one
  tableId.replaceChild(newTbody, tableId.tBodies[0])     
}