【发布时间】:2019-04-02 16:45:28
【问题描述】:
我有一个带有 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>
为什么 Quantity 字段也显示为我不知道的输入字段
因为我的代码有点长,因为我要添加 HTML 表单 Attributes 因为我希望所有数据都到我的后端,所以我在 ajax 调用时序列化我的表单
我已经评论了我正在做什么的所有行,以便大家更容易理解
【问题讨论】:
-
'为什么 Quantity 字段也显示为我不知道的输入字段',这是因为
var quantityField = document.createElement("input"); -
@designtocode 我创建的一个将
AcceptedQty显示为输入字段,这里我说的是Quantity输入字段 -
@dheerajkumar 因为
Quantity和AcceptedQty的值是一样的 -
@CarstenLøvboAndersen 我也这么认为,但问题是总是一样的,一个是可编辑的,另一个是不可食用的
-
@dheerajkumar 这个部分的问题是
tableDataDraft[i]['AcceptedQty'] === tableDataDraft[i][col[j]]。尝试将AcceptedQty的值更改为 4 即可。还为Quantityif 块添加table.innerHTML = tabledata
标签: javascript jquery html html-table