【问题标题】:Loop through my table's specific column循环遍历我的表的特定列
【发布时间】:2016-11-17 22:57:45
【问题描述】:

html

<table id="myList">
           <thead>
               <tr>
                   <td>Product ID</td>
                   <td>Product Name</td>
                   <td>Quantity</td>
               </tr>
           </thead>
           <tbody>

           </tbody>
       </table>

Javascript

var tableRef = document.getElementById("myList").getElementsByTagName("tbody")[0];

       var newRow = tableRef.insertRow(tableRef.rows.length);

       var newCell = newRow.insertCell(0);

       var otherCell = newRow.insertCell(2);

       var check;
       var myText = result.text;
       var myTextTwo =  myText.replace(/['"]+/g, '');
       alert(myTextTwo);

       for (var i = 0; i < tableRef.rows.length; i++) {

           if (myTextTwo != tableRef.rows[i].cells[0].innerHTML) {
               check = true
           }
           else if (myTextTwo == tableRef.rows[i].cells[0].innerHTML) {
               tableRef.rows[i].cells[2].innerHTML += 1;
               check = false;
               break;
           }
       }

       if (check) {
           var newText = document.createTextNode(myTextTwo);
           var otherText = document.createTextNode("1");
           newCell.appendChild(newText);
           otherCell.appendChild(otherText);
       }
       else {
           alert("You have scanned this item before.");
       }

我所做的是扫描包含产品 ID(例如“123”)的 QR,并将产品 ID 插入名为“产品 ID”的列中,我能够做到这一点。

但是,我现在要做的是,如果用户扫描包含相同产品 ID(例如“123”)的二维码,我的代码将能够检测到重复并添加到数量上。

所以我计划做的是遍历“产品 ID”列并检查是否有任何重复。如果没有任何重复,则 Product ID 的 Quantity 将为 1。

Product ID | Product Name | Quantity
  123      |   Hello      |     1

否则存在重复,数量为2。

Product ID | Product Name | Quantity
  123      |   Hello      |     2

【问题讨论】:

  • 扫描得到什么价值或对象?
  • tableRef.rows[i].cells[0].innerHTML 不是已经得到了你想要的吗?
  • 您需要将innerHTML 转换为数字,然后再加1。否则它将进行字符串连接,因此您将得到11 而不是2
  • 我执行了我的代码并遇到了这个错误,IndexSizeError: Failed to execute 'insertCell' on 'HTMLTableRowElement': 提供的值 (2) 超出范围 [-1, 1]。
  • 如果表最初是空的,你的循环将永远不会运行,所以你永远不会到达check = true;。您应该将其初始化为true,然后在找到匹配项时将其设置为false。您不需要查找不匹配项的第一个 if

标签: javascript for-loop html-table


【解决方案1】:

你的意思是这样的吗?

var tableRef = document.getElementById("myList").getElementsByTagName("tbody")[0];

// save the row number of the existing product
var found = false;
var myText = result.text;
var myTextTwo = myText.replace(/['"]+/g, '');

// search the table for the existing product
for (var i = 0; i < tableRef.rows.length && !found; ++i) {
  // if you found it then
  if (tableRef.rows[i].cells[0].innerHTML == myTextTwo) {
    // update the value
    tableRef.rows[i].cells[2].innerHTML += 1;

    // and say we found it
    found = true;
  }
}

// at this point, if we didn't find anything then add a new row
if (!found) {
  var newRow = tableRef.insertRow(tableRef.rows.length);
  newRow.insertCell(0).innerText = "...";
  newRow.insertCell(0).innerText = "...";
  newRow.insertCell(0).innerText = 1;
}

<table id="myList">
  <thead>
    <tr>
      <td>Product ID</td>
      <td>Product Name</td>
      <td>Quantity</td>
    </tr>
  </thead>
  <tbody>

  </tbody>
</table>

【讨论】:

    【解决方案2】:

    我认为这可能会有所帮助:

    函数duplicateCell 返回给定列中包含重复项的行号,如果没有找到重复项,则返回null

    函数addOne需要一个产品ID来检查重复项,一个产品名称(如果没有找到重复项,产品将需要一个名称,否则将被忽略),它还需要知道要处理哪个表.
    AddOne 使用上述函数查找重复项并将数量加一。

    我从 1 辆汽车 1 辆自行车和 1 个滑板开始,添加了 2 辆自行车 (id2) 并添加了 2 枚火箭 (id 4)。

    var duplicateCell = function(tableId, columnNumber, stringToSearch) {
    	var myTbody = document.getElementById(tableId).getElementsByTagName("tbody")[0];
    	var tableRows = myTbody.rows.length;
    	for (var i = 0; i < tableRows; i++) {
    		if (myTbody.rows[i].cells[columnNumber].innerHTML.trim() == stringToSearch)
    			return i
    	}
    	return null;
    }
    
    function addOne(productId, productName, tableId) {
    	var myTbody = document.getElementById(tableId).getElementsByTagName("tbody")[0];
    	var rowIndex = duplicateCell(tableId, 0, productId)
    	if (rowIndex != null) {
    		myTbody.rows[rowIndex].cells[2].innerHTML = +myTbody.rows[rowIndex].cells[2].innerHTML.trim() + 1;
    	} else {
    	var tableRows = myTbody.rows.length;
    		var row = document.getElementById(tableId).insertRow();
    		c1 = row.insertCell(0);
    		c2 = row.insertCell(1);
    		c3 = row.insertCell(2);
    		c1.innerHTML = productId;
    		c2.innerHTML = productName;
    		c3.innerHTML = 1;
    	}
    }
    addOne("4", "Rocket", "myList")
    addOne("4", "Rocket", "myList")
    addOne("2", " ", "myList")
    <table id="myList">
    	<thead>
    		<tr>
    			<td>Product ID</td>
    			<td>Product Name</td>
    			<td>Quantity</td>
    		</tr>
    	</thead>
    	<tbody>
    		<tr>
    			<td>1 </td>
    			<td>Car </td>
    			<td>1 </td>
    		</tr>
    		<tr>
    			<td>2 </td>
    			<td>Bike </td>
    			<td>1 </td>
    		</tr>
    		<tr>
    			<td>3 </td>
    			<td>Skateboard </td>
    			<td>1 </td>
    		</tr>
    	</tbody>
    </table>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-25
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      相关资源
      最近更新 更多