【问题标题】:Unable to read space between the characters无法读取字符之间的空格
【发布时间】: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>
  1. 我收到的错误是因为当类别名称作为礼品包出现时,它没有读取空间。
  2. 将错误视为无效字符

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    这是你的错误:

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

    您正在尝试将"GIFT PACK" 添加为元素的类;但没有类名可以包含空格。例如。当您编写&lt;tr class="GIFT PACK"&gt; 时,该元素将获得两个 类:GIFT 和PACK。当您在 CSS 中编写 .GIFT PACK 时,您正在寻找一个元素 &lt;PACK&gt;,它位于具有类 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:更激进的方法:您可以all item-row,迭代每个,并根据它们应该是什么来设置它们的可见性;无需让浏览器过滤您的项目。

    【讨论】:

    • 是否有任何替代方案,因为在更改选择选项时我会隐藏其他行并显示已选择的类别
    • 为什么需要class?为什么不是属性? tr.setAttribute('data-category', categoryName);那么你可以用var selectedItemRows = document.querySelector('[data-category="' + selectedOption + '"]');代替var selectedItemRows = document.getElementsByClassName(selectedOption);吗?属性不像类那样有限制。
    • 嘿,你能不能添加一些工作的 sn-p,正如我所说的那样,但没有得到预期的输出
    • 呃,应该是querySelectorAll。插入片段。关键是,不应将类用于它们不打算做的事情(保存任意信息)。
    【解决方案2】:

    您的问题似乎出在tr.classList.add(categoryName);

    一个css类不能包含空格,简单明了。

    【讨论】:

    • 那么我能做些什么来克服这个问题,因为我必须在这里提供类或 id,这来自我的数据库,两个字符串之间有空格
    • 使用.replace() 可能会用_ 更改` `。示例:string.replace(" ", "_");
    • @viveksingh 在var categoryName = tableData[i]["Category Name"]; 和tr.classList.add(categoryName); 之间更改您的类别名称空间
    • 要么去掉空格和GIFTPACK有冲突的风险,要么转换为下划线和GIFT_PACK有冲突的风险,或者和GIFT-PACK、GIFT__PACK等一样.或者重新考虑你的整个方法......也许在你从数据库中获得的名称之间建立一个映射表,比如“class0”、“class1”、“class2”等......
    • @Mukyuu @viveksingh ,这还不够。您还需要将其添加到 `var selectedItemRows = document.getElementsByClassName(selectedOption);` 中找到要显示的行
    【解决方案3】:

    替换这个

    tr.classList.add(categoryName);`
    

    有了这个

    tr.classList.add(categoryName.replace(/\s+/g, "-").toLowerCase());
    

    更新

    如果您不想更改类别名称,可以使用 data 属性作为过滤器。

    从添加类列表到添加数据集

    // tr.classList.add(categoryName);
    tr.dataset.category = categoryName;
    

    从按类名选择到按数据集选择

    // var selectedItemRows = document.getElementsByClassName(selectedOption);
    var selectedItemRows = document.querySelectorAll("[data-category='" + selectedOption + "']");
    

      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.dataset.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;
        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.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>

    【讨论】:

    • 嘿,现在当我选择任何下拉菜单时它不会隐藏表格行,因为当用户选择礼品包时,它与 Gift_Pack 不匹配,所以当用户选择下拉菜单时没有形成表格跨度>
    • @viveksingh 我已经更新了我的代码,因为你不能更改类别名称,我建议你使用 data 属性并将其用于过滤。
    【解决方案4】:

    classList.add(tokens...) 不接受 'tokens' 参数中的 ASCII 空格。您可以将空格替换为“-”,就像其他答案中提到的那样。这就是我更改代码的方式(我添加了'replace()'):

    //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.replace(" ", "-"));

    $('#CategoryName').on('change', function() { var selectedOption = this.value.replace(" ", "-"); console.log(selectedOption.replace(" ", "-"));

    The classList attribute’s getter must return a DOMTokenList object whose associated element is the context object and whose associated attribute’s local name is class. The token set of this particular DOMTokenList object are also known as the element’s classes. 来源:https://dom.spec.whatwg.org/#dom-element-classlist

    If token contains any ASCII whitespace, then throw an "InvalidCharacterError" DOMException. 来源:https://dom.spec.whatwg.org/#dom-domtokenlist-add

    【讨论】:

    • 我知道这就是为什么我提出这个问题来寻找替代方案
    猜你喜欢
    • 2016-08-13
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2014-08-21
    相关资源
    最近更新 更多