简体   繁体   English

使用 Javascript 从 HTML 表中删除空行

[英]Deleting null rows from HTML table with Javascript

I have an HTML table that is pulling from a database.我有一个从数据库中提取的 HTML 表。 Some of the rows are coming in with the text null and I would like to loop through this table and delete or hide any rows that have the text null in them.一些行带有文本null ,我想遍历此表并删除或隐藏其中包含文本null 的任何行。

The first column will be the column to loop through and find the 'null' text and delete that row but I can't figure out how to do it.第一列将是循环遍历并找到“空”文本并删除该行的列,但我不知道该怎么做。

 <table class="table table-striped" id="ex-table"> <thead class="thead-inverse"> <col width="120"> <col width="120"> <col width="120"> <tr> <th bgcolor="#feaf3f">Item</th> <th bgcolor="#feaf3f">Price</th> <th bgcolor="#feaf3f">Sale Price</th> </tr> </thead> <tbody> <tr id="tr"> <td id="Item"></td> <td id="Price"></td> <td id="salePrice"></td> </tr> </tbody> </table>

Try the following:请尝试以下操作:

var rowTag = document.getElementsByTagName("TD")
if(rowTag.textContent == "null"){
  rowTag.parentNode.removeChild()
}

如果您碰巧是从不受控制的后端获取数据(因此您无法查询那些不需要的条目),则可以使用document.querySelectorAll('#ex-table tbody td:nth-child(1)')来获取所有第一列单元格document.querySelectorAll('#ex-table tbody td:nth-child(1)')进行迭代,并在将textContent匹配为null时删除整个行.parentNode.remove()

 document.getElementById('cleanup').addEventListener('click', () => [...document.querySelectorAll('#ex-table tbody td:nth-child(1)')].forEach(td => td.textContent == 'null' ? td.parentNode.remove() : true)); 
 <table id="ex-table"><thead><tr><th>Item</th><th>Price</th><th>Sale Price</th></tr></thead><tbody><tr><td>1</td><td>null</td><td>33</td></tr><tr><td>2</td><td>38</td><td>50</td></tr><tr><td>null</td><td>null</td><td>null</td></tr><tr><td>3</td><td>41</td><td>54</td></tr><tr><td>4</td><td>13</td><td>17</td></tr><tr><td>5</td><td>2</td><td>3</td></tr></tbody></table><button id="cleanup">Clean</button> 

Firstly you're using <col> tags in wrong way, you need to use <colgroup> as their parent and place them outside the <thead> tag.首先,您以错误的方式使用<col>标签,您需要使用<colgroup>作为它们的父级并将它们放在<thead>标签之外。 For removing empty rows you should use jQuery, then you need to loop through the rows and then remove it if the first column is empty using td:first-child selector.要删除空行,您应该使用 jQuery,然后您需要遍历行,如果第一列为空,则使用td:first-child选择器将其删除。 look at the snippet.看看片段。

 $("#ex-table tbody tr").each(function() { var html = $(this).find("td:first-child").html(); if (html === 'null') { $(this).remove() } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="table table-striped" id="ex-table"> <colgroup> <col width="120"> <col width="120"> <col width="120"> </colgroup> <thead class="thead-inverse"> <tr> <th bgcolor="#feaf3f">Item</th> <th bgcolor="#feaf3f">Price</th> <th bgcolor="#feaf3f">Sale Price</th> </tr> </thead> <tbody> <tr> <td id="Item">null</td> <td id="Price">50</td> <td id="salePrice">60</td> </tr> <tr id="tr"> <td id="Item">2</td> <td id="Price">60</td> <td id="salePrice">70</td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM