Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript array to table

// dynamic function to cteate table out of 2d arrays
function createTable(element, tableData) {
  
  // creating table elements
  var table = document.createElement('table');
  // creating table body <tbody> element
  var tableBody = document.createElement('tbody');

  // creating rows based on first diamention datas
  tableData.forEach(function(rowData) {
    var row = document.createElement('tr');

    // creating cells in each row based on second diamention datas
    rowData.forEach(function(cellData) {
      var cell = document.createElement('td');
      // adding array item to it's cell
      cell.appendChild(document.createTextNode(cellData));
      // adding the cell to it's row
      row.appendChild(cell);
    });

    // adding each row to table body
    tableBody.appendChild(row);
  });

  // adding table body to table
  table.appendChild(tableBody);
  // adding table to document body
  element.appendChild(table);
}

// example
createTable(
  document.getElementById('my-table'), 
  [
    ['r1, c1', ' r1, c2'], 
    ['r2, c1', ' r2, c2']
  ]
);
Comment

javascript array to table

// dynamic function to cteate table out of 2d arrays
function createTable(element, tableData) {
  
  // creating table elements
  var table = document.createElement('table');
  // creating table body <tbody> element
  var tableBody = document.createElement('tbody');

  // creating rows based on first diamention datas
  tableData.forEach(function(rowData) {
    var row = document.createElement('tr');

    // creating cells in each row based on second diamention datas
    rowData.forEach(function(cellData) {
      var cell = document.createElement('td');
      // adding array item to it's cell
      cell.appendChild(document.createTextNode(cellData));
      // adding the cell to it's row
      row.appendChild(cell);
    });

    // adding each row to table body
    tableBody.appendChild(row);
  });

  // adding table body to table
  table.appendChild(tableBody);
  // adding table to document body
  element.appendChild(table);
}

// example
createTable(
  document.getElementById('my-table'), 
  [
    ['r1, c1', ' r1, c2'], 
    ['r2, c1', ' r2, c2']
  ]
);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript detect if object is date 
Javascript :: iiee javascript 
Javascript :: javascript force precision to 2 decimal numbers 
Javascript :: javascript loop over dictionary 
Javascript :: check url with javascript 
Javascript :: how to change user password firebase 
Javascript :: Removing Service Workers Programmatically 
Javascript :: open cypress window 
Javascript :: js window location relative path 
Javascript :: quine 
Javascript :: padstart javascript 
Javascript :: How to empty a folder in Node.js 
Javascript :: set image size phaser 
Javascript :: styling element using jquery 
Javascript :: jquery scroll to element id 
Javascript :: set js 
Javascript :: jquery set input value 
Javascript :: classname did not match server next js styled components 
Javascript :: refresh javascript 
Javascript :: Javascript how to differentiate single click event and double click event 
Javascript :: javascript setattribute onclick function with parameters 
Javascript :: iterate over list array in solidity 
Javascript :: nodejs: read and write file: use fs and promise 
Javascript :: js get substring before character 
Javascript :: javascript replace hyphen with space 
Javascript :: javascript mouse up mouse down 
Javascript :: onselect javascript 
Javascript :: get value of radio button javascript 
Javascript :: bs modal service close 
Javascript :: slide out navigation 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =