Skip to main content
added 1208 characters in body
Source Link
Nitheesh
  • 20.1k
  • 3
  • 28
  • 52

EDIT: If you want to have a different order, you could keep the order itself in a seperate array and process with that array as in this fiddle. Also to have the sum, select all the Inventory Amount fields from the individual array using Array.map and populate sum using some logic, I used Array.reuce for that.

Working Fiddle

// Array to keep the order of itration
const order = [0, 2, 1, 3, 4];

let products = [
    ["product1", "Small Widget", "159753", 33, 22],
    ["product2", "Medium Widget", "258456", 55, 44],
    ["product3", "Large Widget", "753951", 77, 88],
    ["product4", "Not a Widget", "852654", 11, 2],
    ["product5", "Could be a Widget", "654456", 99, 6],
    ["product6", "Ultimate Widget", "321456", 111, 66],
    ["product7", "Jumbo Small Medium Widget", "987456", 88, 11]
];

//first create the table
function generateTableHead(table, data) {
  let thead = table.createTHead();
  let row = thead.insertRow();
  for (let key of dataorder) {
    let th = document.createElement("th");
    let text = document.createTextNode(keydata[key]);
    th.appendChild(text);
    row.appendChild(th);
  }
}

function generateTable(table, data) {
  for (let element of data) {
    let row = table.insertRow();
    for (key in element) {
      let cell = row.insertCell();
      const textContent = keyorder[key] === '3'3 ? '$' + element[key]element[order[key]] : element[key];element[order[key]];
      let text = document.createTextNode(textContent);
      cell.appendChild(text);
      if (keyorder[key] === "4"4 && element[key]element[order[key]] < 20) {
        cell.style.background = "grey";
      }
    }
  }
}

function generateTableFooter(table) {
  let row = table.insertRow();
  // My summation logic
  // Select the Inventory Amount from each array in products, which is the 5th element of array
  // `map` will generate an array with all Inventory Amount [22, 44, 88, 2, 6, 66, 11]
  // `reduce` will loop through this array and will find the sum
  const total = products.map((product) => product[4]).reduce((sum, curr) => {
    sum += curr;
    return sum;
  }, 0);
  for (let key of order) {
    let cell = row.insertCell();
    const textContent = order[key] === 4 ? total : '';
    let text = document.createTextNode(textContent);
    cell.appendChild(text);
  }
} 

let table = document.querySelector("table");
const header = [ "Product["Product Name", "Product Description", "Product ID", "Price", "Inventory Amount"];
generateTable(table, products);
generateTableHead(table, header);
generateTableFooter(table);
<table id="test">
<id="test"></table>

Working Fiddle

let products = [
    ["product1", "Small Widget", "159753", 33, 22],
    ["product2", "Medium Widget", "258456", 55, 44],
    ["product3", "Large Widget", "753951", 77, 88],
    ["product4", "Not a Widget", "852654", 11, 2],
    ["product5", "Could be a Widget", "654456", 99, 6],
    ["product6", "Ultimate Widget", "321456", 111, 66],
    ["product7", "Jumbo Small Medium Widget", "987456", 88, 11]
];

//first create the table
function generateTableHead(table, data) {
  let thead = table.createTHead();
  let row = thead.insertRow();
  for (let key of data) {
    let th = document.createElement("th");
    let text = document.createTextNode(key);
    th.appendChild(text);
    row.appendChild(th);
  }
}

function generateTable(table, data) {
  for (let element of data) {
    let row = table.insertRow();
    for (key in element) {
      let cell = row.insertCell();
      const textContent = key === '3' ? '$' + element[key] : element[key];
      let text = document.createTextNode(textContent);
      cell.appendChild(text);
      if (key === "4" && element[key] < 20) {
        cell.style.background = "grey";
      }
    }
  }
}
let table = document.querySelector("table");
const header = [ "Product Name", "Product Description", "Product ID", "Price", "Inventory Amount"];
generateTable(table, products);
generateTableHead(table, header);
<table id="test">
</table>

EDIT: If you want to have a different order, you could keep the order itself in a seperate array and process with that array as in this fiddle. Also to have the sum, select all the Inventory Amount fields from the individual array using Array.map and populate sum using some logic, I used Array.reuce for that.

Working Fiddle

// Array to keep the order of itration
const order = [0, 2, 1, 3, 4];

let products = [
  ["product1", "Small Widget", "159753", 33, 22],
  ["product2", "Medium Widget", "258456", 55, 44],
  ["product3", "Large Widget", "753951", 77, 88],
  ["product4", "Not a Widget", "852654", 11, 2],
  ["product5", "Could be a Widget", "654456", 99, 6],
  ["product6", "Ultimate Widget", "321456", 111, 66],
  ["product7", "Jumbo Small Medium Widget", "987456", 88, 11]
];

//first create the table
function generateTableHead(table, data) {
  let thead = table.createTHead();
  let row = thead.insertRow();
  for (let key of order) {
    let th = document.createElement("th");
    let text = document.createTextNode(data[key]);
    th.appendChild(text);
    row.appendChild(th);
  }
}

function generateTable(table, data) {
  for (let element of data) {
    let row = table.insertRow();
    for (key in element) {
      let cell = row.insertCell();
      const textContent = order[key] === 3 ? '$' + element[order[key]] : element[order[key]];
      let text = document.createTextNode(textContent);
      cell.appendChild(text);
      if (order[key] === 4 && element[order[key]] < 20) {
        cell.style.background = "grey";
      }
    }
  }
}

function generateTableFooter(table) {
  let row = table.insertRow();
  // My summation logic
  // Select the Inventory Amount from each array in products, which is the 5th element of array
  // `map` will generate an array with all Inventory Amount [22, 44, 88, 2, 6, 66, 11]
  // `reduce` will loop through this array and will find the sum
  const total = products.map((product) => product[4]).reduce((sum, curr) => {
    sum += curr;
    return sum;
  }, 0);
  for (let key of order) {
    let cell = row.insertCell();
    const textContent = order[key] === 4 ? total : '';
    let text = document.createTextNode(textContent);
    cell.appendChild(text);
  }
} 

let table = document.querySelector("table");
const header = ["Product Name", "Product Description", "Product ID", "Price", "Inventory Amount"];
generateTable(table, products);
generateTableHead(table, header);
generateTableFooter(table);
<table id="test"></table>
deleted 10 characters in body
Source Link
Nitheesh
  • 20.1k
  • 3
  • 28
  • 52

Please find the correction that I added

  • Your table header is not bounded correctly. I created a dummy array with your required header labels and passed to your generateTableHead function
  • Inside your generateTable function, there should be some logic to differentiate "Price" and "Inventory Amount" data. I did it using the index of the array. Index 3 belongs to Price and 4 belongs to Inventory Amount.
  • So if the index is greater than 23, append $ symbol, and if index is 4 add a background color to cell based on the value.

Working Fiddle

let products = [
    ["product1", "Small Widget", "159753", 33, 22],
    ["product2", "Medium Widget", "258456", 55, 44],
    ["product3", "Large Widget", "753951", 77, 88],
    ["product4", "Not a Widget", "852654", 11, 2],
    ["product5", "Could be a Widget", "654456", 99, 6],
    ["product6", "Ultimate Widget", "321456", 111, 66],
    ["product7", "Jumbo Small Medium Widget", "987456", 88, 11]
];

//first create the table
function generateTableHead(table, data) {
  let thead = table.createTHead();
  let row = thead.insertRow();
  for (let key of data) {
    let th = document.createElement("th");
    let text = document.createTextNode(key);
    th.appendChild(text);
    row.appendChild(th);
  }
}

function generateTable(table, data) {
  for (let element of data) {
    let row = table.insertRow();
    for (key in element) {
      let cell = row.insertCell();
      const textContent = +keykey >=== 2'3' ? '$' + element[key] : element[key];
      let text = document.createTextNode(textContent);
      cell.appendChild(text);
      if (key === "4" && element[key] < 20) {
        cell.style.background = "grey";
      }
    }
  }
}
let table = document.querySelector("table");
const header = [ "Product Name", "Product Description", "Product ID", "Price", "Inventory Amount"];
generateTable(table, products);
generateTableHead(table, header);
<table id="test">
</table>

Please find the correction that I added

  • Your table header is not bounded correctly. I created a dummy array with your required header labels and passed to your generateTableHead function
  • Inside your generateTable function, there should be some logic to differentiate "Price" and "Inventory Amount" data. I did it using the index of the array. Index 3 belongs to Price and 4 belongs to Inventory Amount.
  • So if the index is greater than 2, append $ symbol, and if index is 4 add a background color to cell based on the value.

Working Fiddle

let products = [
    ["product1", "Small Widget", "159753", 33, 22],
    ["product2", "Medium Widget", "258456", 55, 44],
    ["product3", "Large Widget", "753951", 77, 88],
    ["product4", "Not a Widget", "852654", 11, 2],
    ["product5", "Could be a Widget", "654456", 99, 6],
    ["product6", "Ultimate Widget", "321456", 111, 66],
    ["product7", "Jumbo Small Medium Widget", "987456", 88, 11]
];

//first create the table
function generateTableHead(table, data) {
  let thead = table.createTHead();
  let row = thead.insertRow();
  for (let key of data) {
    let th = document.createElement("th");
    let text = document.createTextNode(key);
    th.appendChild(text);
    row.appendChild(th);
  }
}

function generateTable(table, data) {
  for (let element of data) {
    let row = table.insertRow();
    for (key in element) {
      let cell = row.insertCell();
      const textContent = +key > 2 ? '$' + element[key] : element[key];
      let text = document.createTextNode(textContent);
      cell.appendChild(text);
      if (key === "4" && element[key] < 20) {
        cell.style.background = "grey";
      }
    }
  }
}
let table = document.querySelector("table");
const header = [ "Product Name", "Product Description", "Product ID", "Price", "Inventory Amount"];
generateTable(table, products);
generateTableHead(table, header);
<table id="test">
</table>

Please find the correction that I added

  • Your table header is not bounded correctly. I created a dummy array with your required header labels and passed to your generateTableHead function
  • Inside your generateTable function, there should be some logic to differentiate "Price" and "Inventory Amount" data. I did it using the index of the array. Index 3 belongs to Price and 4 belongs to Inventory Amount.
  • So if the index is 3, append $ symbol, and if index is 4 add a background color to cell based on the value.

Working Fiddle

let products = [
    ["product1", "Small Widget", "159753", 33, 22],
    ["product2", "Medium Widget", "258456", 55, 44],
    ["product3", "Large Widget", "753951", 77, 88],
    ["product4", "Not a Widget", "852654", 11, 2],
    ["product5", "Could be a Widget", "654456", 99, 6],
    ["product6", "Ultimate Widget", "321456", 111, 66],
    ["product7", "Jumbo Small Medium Widget", "987456", 88, 11]
];

//first create the table
function generateTableHead(table, data) {
  let thead = table.createTHead();
  let row = thead.insertRow();
  for (let key of data) {
    let th = document.createElement("th");
    let text = document.createTextNode(key);
    th.appendChild(text);
    row.appendChild(th);
  }
}

function generateTable(table, data) {
  for (let element of data) {
    let row = table.insertRow();
    for (key in element) {
      let cell = row.insertCell();
      const textContent = key === '3' ? '$' + element[key] : element[key];
      let text = document.createTextNode(textContent);
      cell.appendChild(text);
      if (key === "4" && element[key] < 20) {
        cell.style.background = "grey";
      }
    }
  }
}
let table = document.querySelector("table");
const header = [ "Product Name", "Product Description", "Product ID", "Price", "Inventory Amount"];
generateTable(table, products);
generateTableHead(table, header);
<table id="test">
</table>

Source Link
Nitheesh
  • 20.1k
  • 3
  • 28
  • 52

Please find the correction that I added

  • Your table header is not bounded correctly. I created a dummy array with your required header labels and passed to your generateTableHead function
  • Inside your generateTable function, there should be some logic to differentiate "Price" and "Inventory Amount" data. I did it using the index of the array. Index 3 belongs to Price and 4 belongs to Inventory Amount.
  • So if the index is greater than 2, append $ symbol, and if index is 4 add a background color to cell based on the value.

Working Fiddle

let products = [
    ["product1", "Small Widget", "159753", 33, 22],
    ["product2", "Medium Widget", "258456", 55, 44],
    ["product3", "Large Widget", "753951", 77, 88],
    ["product4", "Not a Widget", "852654", 11, 2],
    ["product5", "Could be a Widget", "654456", 99, 6],
    ["product6", "Ultimate Widget", "321456", 111, 66],
    ["product7", "Jumbo Small Medium Widget", "987456", 88, 11]
];

//first create the table
function generateTableHead(table, data) {
  let thead = table.createTHead();
  let row = thead.insertRow();
  for (let key of data) {
    let th = document.createElement("th");
    let text = document.createTextNode(key);
    th.appendChild(text);
    row.appendChild(th);
  }
}

function generateTable(table, data) {
  for (let element of data) {
    let row = table.insertRow();
    for (key in element) {
      let cell = row.insertCell();
      const textContent = +key > 2 ? '$' + element[key] : element[key];
      let text = document.createTextNode(textContent);
      cell.appendChild(text);
      if (key === "4" && element[key] < 20) {
        cell.style.background = "grey";
      }
    }
  }
}
let table = document.querySelector("table");
const header = [ "Product Name", "Product Description", "Product ID", "Price", "Inventory Amount"];
generateTable(table, products);
generateTableHead(table, header);
<table id="test">
</table>