【发布时间】:2019-02-03 04:50:20
【问题描述】:
我有 JSON 数据,我试图通过这些数据填充 HTML 表。之前表格填充得很好,但根据我的要求,我已经更改了我的代码。
我在做什么
- 我有一个
form,里面有一个选择选项(下拉菜单)和我的表格 - 在创建表时,我正在向每个项目行添加一个带有类别名称的类
- 因此,当用户选择任何下拉选项时,我只显示该类别的数据并隐藏其他类别的数据,但在添加类时出现错误
- 错误是
InvalidCharacterError: String contains an invalid character - 我不知道我做错了什么
片段
var tableData = [{
"Item Code": "1978",
"Item Name": "Alu Chat-S",
"Selling Price": "55.0000",
"Category Name": "Chats"
},
{
"Item Code": "1979",
"Item Name": "Dahi Alu Chat-S",
"Selling Price": "50.0000",
"Category Name": "Chats"
},
{
"Item Code": "1980",
"Item Name": "Samosa-S",
"Selling Price": "25.0000",
"Category Name": "Chats"
},
{
"Item Code": "1981",
"Item Name": "SamosaChat-S",
"Selling Price": "40.0000",
"Category Name": "Chats"
},
{
"Item Code": "1982",
"Item Name": "Dahi Samosa Chats-S",
"Selling Price": "55.0000",
"Category Name": "Chats"
},
{
"Item Code": "1983",
"Item Name": "Garam Samosa Chats-S",
"Selling Price": "55.0000",
"Category Name": "Chats"
},
{
"Item Code": "1984",
"Item Name": "Kachori Chats-S",
"Selling Price": "55.0000",
"Category Name": "Chats"
},
{
"Item Code": "1985",
"Item Name": "Garam Kachori Chat-S",
"Selling Price": "50.0000",
"Category Name": "Chats"
},
{
"Item Code": "1986",
"Item Name": "Dahi Kachori Chat-S",
"Selling Price": "55.0000",
"Category Name": "Chats"
},
{
"Item Code": "1987",
"Item Name": "Dai Raj Kachori-S",
"Selling Price": "65.0000",
"Category Name": "Chats"
},
{
"Item Code": "1988",
"Item Name": "Baby Kachori Chat-S",
"Selling Price": "65.0000",
"Category Name": "Chats"
},
{
"Item Code": "1989",
"Item Name": "Dahi Baby Kachori-S",
"Selling Price": "65.0000",
"Category Name": "Chats"
},
{
"Item Code": "1990",
"Item Name": "Anar Bhalla-S",
"Selling Price": "65.0000",
"Category Name": "Chats"
},
{
"Item Code": "1991",
"Item Name": "Dahi Bhalla-S",
"Selling Price": "65.0000",
"Category Name": "Chats"
},
{
"Item Code": "1992",
"Item Name": "Jhal Muri-S",
"Selling Price": "60.0000",
"Category Name": "Chats"
},
{
"Item Code": "1993",
"Item Name": "Chat Platter-S",
"Selling Price": "110.0000",
"Category Name": "Chats"
},
{
"Item Code": "1994",
"Item Name": "Dahi Papdi Chat-S",
"Selling Price": "55.0000",
"Category Name": "Chats"
},
{
"Item Code": "2402",
"Item Name": "ALMOND CHBAR",
"Selling Price": "26.2000",
"Category Name": "GIFT PACK"
},
{
"Item Code": "333",
"Item Name": "A BR SB EX",
"Selling Price": "1.0000",
"Category Name": "EXEMPTED"
}
]
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.
var colNum = col.length; //to improve the speed
for (var i = 0; i < colNum + 1; i++) {
var th = document.createElement("th"); // TABLE HEADER.
if (i >= colNum) {
th.innerHTML = "Quantity";
tr.appendChild(th);
tr.classList.add("text-center");
tr.classList.add("head")
} else {
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);
tr.classList.add("item-row");
for (var j = 0; j < col.length + 1; j++) {
//here i am adding a class with the name of the category to each items row.
var categoryName = tableData[i]["Category Name"];
tr.classList.add(categoryName);
let tabCell = tr.insertCell(-1);
var hiddenField = document.createElement("input");
hiddenField.style.display = "none";
var tabledata = tableData[i][col[j]];
if (i > -1 && j >= colNum) {
var quantityField = document.createElement("input");
quantityField.style.border = "none";
quantityField.style["text-align"] = "center";
quantityField.setAttribute('name', 'Quantity');
quantityField.setAttribute('autocomplete', 'on');
quantityField.setAttribute('value', '0');
quantityField.setAttribute('type', 'number');
quantityField.setAttribute('required', 'required');
quantityField.classList.add("dataReset");
tabCell.appendChild(quantityField);
} else {
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 (tableData[i]['Selling Price'] === tableData[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Selling_Price');
hiddenField.setAttribute('value', tabledata);
tabCell.appendChild(hiddenField);
}
if (tableData[i]['Category Name'] === tableData[i][col[j]]) {
tabCell.innerHTML = tabledata;
hiddenField.setAttribute('name', 'Category_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);
$('#CategoryName').on('change', function() {
var selectedOption = this.value;
console.log(selectedOption);
//getting all item rows so i can target them.
var itemRows = document.getElementsByClassName("item-row");
if (selectedOption === 'All') {
//If "All" then style all rows with visibility: visible.
for (var i = 0; i < itemRows.length; i++) {
itemRows[i].style.visibility = "visible";
}
} else {
//If the selectedOption is anything other than "All",
// firstly i am style all rows with visibility: collapse
for (var i = 0; i < itemRows.length; i++) {
itemRows[i].style.visibility = "collapse";
}
// then getting all rows which have the selectedOption as a class and style those rows with visibility: visible.
var selectedItemRows = document.getElementsByClassName(selectedOption);
for (var i = 0; i < selectedItemRows.length; i++) {
selectedItemRows[i].style.visibility = "visible";
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container" id="divHide">
<form action="InsertQuantityIndent" method="post" id="form1" autocomplete="on">
<div class="row position-relative">
<div class="col-lg-4">
<h5 id="commonHeader">Category</h5>
<select class="test" id="CategoryName" name="categoryCode">
<option>All</option>
<option>Chats</option>
<option>GIFT PACK</option>
<option>EXEMPTED</option>
</select>
</div>
</div>
<hr style="border: 1px solid black">
<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>
<button id="clear">
<i class="fas fa-eraser"></i> Clear
</button>
<!-- <button id="print" type="button" onclick="printFunction()">
<i class="fas fa-print"></i> Print
</button> -->
</div>
</form>
</div>
- 我收到的错误是因为当类别名称作为礼品包出现时,它没有读取空间。
- 将错误视为无效字符
【问题讨论】:
标签: javascript html