我有一个包含 JSON 数据的 HTML 表,我正在做的是创建一列作为输入字段,我的表标题是 Code
,Item Name
,Unitcode
、Quantity
和AcceptedQty
,其中我仅将接受数量设置为输入字段,但数量字段也会转换为输入字段,但我不这样做知道我做错了什么
var tableDataDraft = [{
"Code": "1326",
"Item Name": "PINEAPPLE KG",
"Unitcode": "NOS",
"Quantity": "3.00",
"AcceptedQty": "3.00"
},
{
"Code": "1494",
"Item Name": "2D CAKE CHARGES PER KG",
"Unitcode": "NOS",
"Quantity": "3.00",
"AcceptedQty": "3.00"
}
]
function addTableDraft(tableDataDraft) {
var col = Object.keys(tableDataDraft[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);
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th");
th.innerHTML = col[i];
tr.appendChild(th);
tr.classList.add("text-center");
tr.classList.add("head")
}
for (var i = 0; i < tableDataDraft.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
let tabCell = tr.insertCell(-1);
var hiddenField = document.createElement("input"); //creating input field hidden
hiddenField.style.display = "none";
var tabledata = tableDataDraft[i][col[j]];
if (tableDataDraft[i]['Code'] === tableDataDraft[i][col[j]]) { //now setting html attributes
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Code');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableDataDraft[i]['Item Name'] === tableDataDraft[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Name');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableDataDraft[i]['Unitcode'] === tableDataDraft[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Unit_code');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableDataDraft[i]['Quantity'] === tableDataDraft[i][col[j]]) { //this quantity field i don't want to be input field
hiddenField.setAttribute('name', 'Quantity');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableDataDraft[i]['AcceptedQty'] === tableDataDraft[i][col[j]]) { //this one i want to be a input field
var quantityField = document.createElement("input");
quantityField.style.border = "none";
quantityField.style["text-align"] = "right";
quantityField.setAttribute("name", "AcceptedQty");
quantityField.setAttribute("autocomplete", "on");
quantityField.setAttribute("value", tabledata);
quantityField.setAttribute("type", "tel");
quantityField.setAttribute("required", "required");
quantityField.classList.add("dataReset");
quantityField.toLocaleString('en-IN');
tabCell.appendChild(quantityField);
}
if (j > 1)
tabCell.classList.add("text-right");
}
}
var divContainer = document.getElementById("table");
divContainer.innerHTML = "";
divContainer.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
addTableDraft(tableDataDraft)
<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">
<div class="table-responsive" id="commonDvScroll">
<table id=table></table>
</div>
为什么数量字段也显示为输入字段,我不知道
由于我的代码有点长,因为我添加了 HTML 表单 Attributes
因为我希望将所有数据发送到我的后端,所以我在 ajax 调用时序列化我的表单
我已经注释了我正在做什么的所有行,以便大家更容易理解
最佳答案
问题是因为在每行插入值时,您比较的是列值而不是列名称。当值相同时,这会导致问题,例如 Quantity
和 AcceptedQty
具有相同的值 3.0
。尝试将其更改为 4.0
,您会发现它有效。
这是代码的简化版本,用于检查当前列是否为 AcceptedQty
并仅显示一个输入字段。您仍然可以使用其他 if
block ,但请确保条件类似于 if (col[j] === 'Code')
或 (col[ j] === '数量')
等
var tableDataDraft = [{
"Code": "1326",
"Item Name": "PINEAPPLE KG",
"Unitcode": "NOS",
"Quantity": "3.00",
"AcceptedQty": "3.00"
},
{
"Code": "1494",
"Item Name": "2D CAKE CHARGES PER KG",
"Unitcode": "NOS",
"Quantity": "3.00",
"AcceptedQty": "3.00"
}
]
function addTableDraft(tableDataDraft) {
var col = Object.keys(tableDataDraft[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);
for (var i = 0; i < col.length; i++) {
var th = document.createElement("th");
th.innerHTML = col[i];
tr.appendChild(th);
tr.classList.add("text-center");
tr.classList.add("head")
}
for (var i = 0; i < tableDataDraft.length; i++) {
tr = table.insertRow(-1);
for (var j = 0; j < col.length; j++) {
let tabCell = tr.insertCell(-1);
var hiddenField = document.createElement("input"); //creating input field hidden
hiddenField.style.display = "none";
var tabledata = tableDataDraft[i][col[j]];
if (col[j] === 'AcceptedQty') {
//this one i want to be a input field
var quantityField = document.createElement("input");
quantityField.style.border = "none";
quantityField.style["text-align"] = "right";
quantityField.setAttribute("name", "AcceptedQty");
quantityField.setAttribute("autocomplete", "on");
quantityField.setAttribute("value", tabledata);
quantityField.setAttribute("type", "tel");
quantityField.setAttribute("required", "required");
quantityField.classList.add("dataReset");
quantityField.toLocaleString('en-IN');
tabCell.appendChild(quantityField);
}
else
{ //now setting html attributes
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Item_Code');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (j > 1)
tabCell.classList.add("text-right");
}
}
var divContainer = document.getElementById("table");
divContainer.innerHTML = "";
divContainer.appendChild(table);
table.classList.add("table");
table.classList.add("table-striped");
table.classList.add("table-bordered");
table.classList.add("table-hover");
}
addTableDraft(tableDataDraft)
<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">
<div class="table-responsive" id="commonDvScroll">
<table id=table></table>
</div>
关于javascript - HTML 表格无法正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55468128/