gpt4 book ai didi

javascript - 无法读取字符之间的空格

转载 作者:行者123 更新时间:2023-11-30 14:11:22 25 4
gpt4 key购买 nike

我有 JSON 数据,我试图用它来填充 HTML 表。早些时候表格填充正常,但根据我的要求,我更改了我的代码。

我在做什么

  • 我有一个表单,其中有一个选择选项(下拉列表)和我的表格
  • 在创建表格时,我将一个带有类别名称的类添加到每个项目行
  • 因此,当用户选择任何下拉选项时,我只显示该类别的数据并隐藏其他数据,但在添加类时出现错误
  • 错误是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>

  1. 我收到的错误是因为当类别名称作为礼品包出现时,它没有读取空间。
  2. 它给出的错误是无效字符

最佳答案

这是你的错误:

// ...
"Category Name": "GIFT PACK"
// ...
var categoryName = tableData[i]["Category Name"];
tr.classList.add(categoryName)

您正在尝试添加 "GIFT PACK"作为元素的一类;但类名不能包含空格。例如。当你写 <tr class="GIFT PACK"> ,该元素获得两个类:GIFTPACK .当你写 .GIFT PACK在 CSS 中,您正在寻找一个元素 <PACK>在类 GIFT 的元素内.类名不能包含空格,并且 JavaScript 报告错误,因为您强制它做一些不可能的事情。

编辑:根据评论。

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.setAttribute('data-category', 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;
//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.querySelectorAll('[data-category="' + 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>

EDIT2:更激进的方法:你可以得到所有 item-row ,迭代每一个,并根据它们应该是什么来设置它们的可见性;无需让浏览器过滤您的项目。

关于javascript - 无法读取字符之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54396181/

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