gpt4 book ai didi

javascript - 如何使用 JavaScript 手动将列​​插入到 HTML 表中?

转载 作者:行者123 更新时间:2023-11-30 06:19:13 26 4
gpt4 key购买 nike

我有一个用 JSON 数据呈现的 HTML 表,我想做的是使用 JavaScript 创建一个列,该列不是 json 数据

  • 我有一个包含 2 列的表格,我想手动添加第 3 列作为 Quantity
  • 使用 JavaScript 可以做到,但我不明白这一点

我的片段

var tableData = [
{ "Item Code": "1001", "Item Name": "Beverages", },
{ "Item Code": "2003", "Item Name": "Juices", },
{ "Item Code": "1004", "Item Name": "Soups", },
{ "Item Code": "2005", "Item Name": "Cookies", },
];

function addTable(tableData) {
var col = Object.keys(tableData[0]);
var countNum = col.filter(i => !isNaN(i)).length;
var num = col.splice(0, countNum);
col = col.concat(num);
var table = document.createElement("table");
var tr = table.insertRow(-1); // TABLE ROW.
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th"); // TABLE HEADER.
th.innerHTML = col[i];
tr.appendChild(th);
tr.classList.add("text-center");
tr.classList.add("head")
}
for (var i = 0; i < tableData.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
let tabCell = tr.insertCell(-1);
var hiddenField = document.createElement("input");
hiddenField.style.display = "none";
var tabledata = tableData[i][col[j]];

if (tableData[i]['Item Code'] === tableData[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Code');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableData[i]['Item Name'] === tableData[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Name');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (j > 1)
tabCell.classList.add("text-right");
}
}
var divContainer = document.getElementById("HourlysalesSummary");
divContainer.innerHTML = "";
divContainer.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
addTable(tableData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<form action="InsertQuantityIndent" method="post" id="form1">
<div class="row position-relative">

<br>
<div class="table-responsive">
<table class=" w-100" id=HourlysalesSummary></table>
</div>
<div>
<button type="submit" id="save">
<i class="fas fa-save"></i> Save
</button>
</div>
</form>

我想添加名为 quantity 的第三列,数据为 0。我不知道我该怎么做。

最佳答案

它会帮助你。它会在页面加载时自行添加列。

var tableData = [
{ "Item Code": "1001", "Item Name": "Beverages", },
{ "Item Code": "2003", "Item Name": "Juices", },
{ "Item Code": "1004", "Item Name": "Soups", },
{ "Item Code": "2005", "Item Name": "Cookies", },
];

function addTable(tableData) {
var col = Object.keys(tableData[0]);
var countNum = col.filter(i => !isNaN(i)).length;
var num = col.splice(0, countNum);
col = col.concat(num);
var table = document.createElement("table");
var tr = table.insertRow(-1); // TABLE ROW.
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th"); // TABLE HEADER.
th.innerHTML = col[i];
tr.appendChild(th);
tr.classList.add("text-center");
tr.classList.add("head")
}
for (var i = 0; i < tableData.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
let tabCell = tr.insertCell(-1);
var hiddenField = document.createElement("input");
hiddenField.style.display = "none";
var tabledata = tableData[i][col[j]];

if (tableData[i]['Item Code'] === tableData[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Code');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableData[i]['Item Name'] === tableData[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Name');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (j > 1)
tabCell.classList.add("text-right");
}
}
var divContainer = document.getElementById("HourlysalesSummary");
divContainer.innerHTML = "";
divContainer.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
addTable(tableData);

function addCol(){

var colname = "Quantity";
var colValue = 0;

var tablehead = $("#HourlysalesSummary tr[class='text-center head']")[0].append($('<th>'+colname+'</th>')[0]);
var tableRow = $("#HourlysalesSummary tr");
var i =1;

while(i < tableRow.length){

tableRow[i].append($('<td>'+colValue+'</td>')[0]);
i++;

}

}

addCol();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css"/>


<form action="InsertQuantityIndent" method="post" id="form1">
<div class="row position-relative">

<br>
<div class="table-responsive">
<table class=" w-100" id="HourlysalesSummary"></table>
</div>
<div>
<button type="submit" id="save">
<i class="fas fa-save"></i> Save
</button>
</div>
</form>

关于javascript - 如何使用 JavaScript 手动将列​​插入到 HTML 表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54237003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com