javascript-如果值为0,如何隐藏HTML表行

前端之家收集整理的这篇文章主要介绍了javascript-如果值为0,如何隐藏HTML表行 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个包含4列的HTML表,项目代码名称类别名称数量输入字段的数量

我正在用JSON数据填充表,最初将输入字段设置为0

用户由于大量数据而在输入字段中输入某些内容时,用户想要查看他为此输入的内容,我提供了一个查看按钮,当用户单击该按钮并且我在同一表格中显示所有非零行时,则单击视图后,我将隐藏视图并显示编辑以供用户再次编辑,但是单击输入字段的所有数据后,重置为0

>我想当用户单击以编辑数据时,应该在输入字段中输入用户之前输入的内容

片段

  1. var tableData = [{
  2. "Item Code": "1978","Item Name": "Alu Chat-S","Category Name": "Chats"
  3. },{
  4. "Item Code": "1979","Item Name": "Dahi Alu Chat-S",{
  5. "Item Code": "1980","Item Name": "Samosa-S",{
  6. "Item Code": "1981","Item Name": "SamosaChat-S",{
  7. "Item Code": "1982","Item Name": "Dahi Samosa Chats-S","Category Name": "regular"
  8. },{
  9. "Item Code": "1983","Item Name": "Garam Samosa Chats-S",{
  10. "Item Code": "1984","Item Name": "Kachori Chats-S",{
  11. "Item Code": "1985","Item Name": "Garam Kachori Chat-S",{
  12. "Item Code": "1986","Item Name": "Dahi Kachori Chat-S","Category Name": "fastfood"
  13. },{
  14. "Item Code": "1987","Item Name": "Dai Raj Kachori-S",{
  15. "Item Code": "1988","Item Name": "Baby Kachori Chat-S",{
  16. "Item Code": "1989","Item Name": "Dahi Baby Kachori-S",{
  17. "Item Code": "1990","Item Name": "Anar Bhalla-S",{
  18. "Item Code": "1991","Item Name": "Dahi Bhalla-S",{
  19. "Item Code": "1992","Item Name": "Jhal Muri-S",{
  20. "Item Code": "1993","Item Name": "Chat Platter-S",{
  21. "Item Code": "1994","Item Name": "Dahi Papdi Chat-S","Category Name": "GIFT PACK"
  22. },{
  23. "Item Code": "2402","Item Name": "ALMOND CHBAR",{
  24. "Item Code": "333","Item Name": "A BR SB EX","Category Name": "EXEMPTED"
  25. },{
  26. "Item Code": "603","Item Name": "AMUL FRESH CREAM","Category Name": "EXEMPTED"
  27. }
  28. ]
  29. function addTable(tableData) {
  30. var col = Object.keys(tableData[0]);
  31. var countNum = col.filter(i => !isNaN(i)).length;
  32. var num = col.splice(0,countNum);
  33. col = col.concat(num);
  34. var table = document.createElement("table");
  35. var tr = table.insertRow(-1); // TABLE ROW.
  36. var colNum = col.length; //to improve the speed
  37. for (var i = 0; i < colNum + 1; i++) {
  38. var th = document.createElement("th"); // TABLE HEADER.
  39. if (i >= colNum) {
  40. th.innerHTML = "Quantity";
  41. tr.appendChild(th);
  42. tr.classList.add("text-center");
  43. tr.classList.add("head");
  44. } else {
  45. th.innerHTML = col[i];
  46. tr.appendChild(th);
  47. tr.classList.add("text-center");
  48. tr.classList.add("head");
  49. }
  50. }
  51. for (var i = 0; i < tableData.length; i++) {
  52. tr = table.insertRow(-1);
  53. tr.classList.add("item-row");
  54. for (var j = 0; j < col.length + 1; j++) {
  55. //here i am adding a class with the name of the category to each items row.
  56. var categoryName = tableData[i]["Category Name"];
  57. tr.dataset.category = categoryName;
  58. let tabCell = tr.insertCell(-1);
  59. var hiddenField = document.createElement("input");
  60. hiddenField.style.display = "none";
  61. var quantityField = document.createElement("input");
  62. var tabledata = tableData[i][col[j]];
  63. if (i > -1 && j >= colNum) {
  64. quantityField.style.border = "none";
  65. quantityField.style["text-align"] = "right";
  66. quantityField.setAttribute("name","Quantity");
  67. quantityField.setAttribute("autocomplete","on");
  68. quantityField.setAttribute("value","0");
  69. quantityField.setAttribute("type","number");
  70. quantityField.setAttribute("required","required");
  71. quantityField.classList.add("dataReset");
  72. quantityField.toLocaleString('en-IN');
  73. tabCell.appendChild(quantityField);
  74. } else {
  75. if (tableData[i]["Item Code"] === tableData[i][col[j]]) {
  76. tabCell.innerHTML = tabledata;
  77. hiddenField.setAttribute("name","Item_Code");
  78. hiddenField.setAttribute("value",tabledata);
  79. tabCell.appendChild(hiddenField);
  80. }
  81. if (tableData[i]["Item Name"] === tableData[i][col[j]]) {
  82. tabCell.innerHTML = tabledata;
  83. hiddenField.setAttribute("name","Item_Name");
  84. hiddenField.setAttribute("value",tabledata);
  85. tabCell.appendChild(hiddenField);
  86. }
  87. if (tableData[i]["Category Name"] === tableData[i][col[j]]) {
  88. tabCell.innerHTML = tabledata;
  89. hiddenField.setAttribute("name","Category_Name");
  90. hiddenField.setAttribute("value",tabledata);
  91. tabCell.appendChild(hiddenField);
  92. }
  93. if (j > 1) tabCell.classList.add("text-right");
  94. }
  95. }
  96. }
  97. var divContainer = document.getElementById("HourlysalesSummary");
  98. divContainer.innerHTML = "";
  99. divContainer.appendChild(table);
  100. table.classList.add("table");
  101. table.classList.add("table-striped");
  102. table.classList.add("table-bordered");
  103. table.classList.add("table-hover");
  104. $("#view").on("click",function() {
  105. var itemRows = document.getElementsByClassName("item-row");
  106. if (quantityField === 0) {
  107. tabCell.innerHTML = tabledata.hide();;
  108. }
  109. /* $("#HourlysalesSummary tr td").filter(function(){
  110. return $(this).text() == 0;
  111. }).hide(); */
  112. });
  113. }
  114. addTable(tableData);
  115. var selectedOption = "";
  116. $("#CategoryName").on("change",function() {
  117. selectedOption = this.value;
  118. //getting all item rows so i can target them.
  119. var itemRows = document.getElementsByClassName("item-row");
  120. if (selectedOption === "All") {
  121. //If "All" then style all rows with visibility: visible.
  122. for (var i = 0; i < itemRows.length; i++) {
  123. itemRows[i].style.visibility = "visible";
  124. }
  125. } else {
  126. //If the selectedOption is anything other than "All",// firstly i am style all rows with visibility: collapse
  127. for (var i = 0; i < itemRows.length; i++) {
  128. itemRows[i].style.visibility = "collapse";
  129. }
  130. /* alert(itemRows); */
  131. // then getting all rows which have the selectedOption as a class and style those rows with visibility: visible.
  132. var selectedItemRows = document.querySelectorAll("[data-category='" + selectedOption + "']");
  133. for (var i = 0; i < selectedItemRows.length; i++) {
  134. selectedItemRows[i].style.visibility = "visible";
  135. }
  136. }
  137. });
  138. function view() {
  139. //get all quantity input fields
  140. var quantityFields = document.getElementsByClassName("dataReset");
  141. //iterate through all quantity input fields
  142. for (var i = 0; i < quantityFields.length; i++) {
  143. if (quantityFields[i].value != 0) {
  144. //if the input value of this quantity field is not equal to zero then find the closest "item-row"
  145. //so that we can set this table row to visible
  146. quantityFields[i].closest(".item-row").style.visibility = "visible";
  147. } else {
  148. //if the input value of this quantity field is equal to zero then find the closest "item-row"
  149. //so that we can set this table row to collapse
  150. quantityFields[i].closest(".item-row").style.display = "none";
  151. }
  152. }
  153. //change the value of the select menu to "All"
  154. $('#CategoryName').val('All');
  155. $('#see').hide();
  156. $('#edit').show();
  157. }
  158. function edit1() {
  159. addTable(tableData);
  160. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
  3. <div class="container" id="divHide">
  4. <form id="indentForm" autocomplete="on">
  5. <div class="row position-relative">
  6. <div class="col-lg-4">
  7. <h5 id="commonHeader">Category</h5>
  8. <select class="test" id="CategoryName" name="categoryCode">
  9. <option>All</option>
  10. <option>Chats</option>
  11. <option>regular</option>
  12. <option>fastfood</option>
  13. <option>GIFT PACK</option>
  14. <option>EXEMPTED</option>
  15. </select>
  16. </div>
  17. </div>
  18. <hr style="border: 1px solid black">
  19. <div class="table-responsive">
  20. <table class="w-100" id=HourlysalesSummary></table>
  21. </div>
  22. <div>
  23. <button type="submit" id="save">
  24. Save
  25. </button>
  26. <button id="see" type="button" onclick="view()">view</button>
  27. <button id="edit" type="button" onclick="edit1()" style="display:none">edit</button>
  28. </div>
  29. </form>
  30. </div>

我想当用户点击编辑之前应该在视图之前出现的用户输入的数据

最佳答案
按照其余代码的结构,我将使用CSS可见性属性来隐藏和显示行.

您可以通过它们的className获取所有数量输入字段,并检查该值是否为零.

如果其不为零,则将项目行的CSS设置为可见.
如果它为零,则将项目行的CSS设置为折叠.

  1. function view(){
  2. //get all quantity input fields
  3. var quantityFields = document.getElementsByClassName("dataReset");
  4. //iterate through all quantity input fields
  5. for(var i = 0; i < quantityFields.length; i++){
  6. if(quantityFields[i].value != 0){
  7. //if the input value of this quantity field is not equal to zero then find the closest "item-row"
  8. //so that we can set this table row to visible
  9. quantityFields[i].closest(".item-row").style.visibility = "visible";
  10. }else{
  11. //if the input value of this quantity field is equal to zero then find the closest "item-row"
  12. //so that we can set this table row to collapse
  13. quantityFields[i].closest(".item-row").style.visibility = "collapse";
  14. }
  15. }
  16. //change the value of the select menu to "All"
  17. $('#CategoryName').val('All');
  18. }

猜你在找的HTML相关文章