简体   繁体   English

如何使用两个按钮过滤表格的列类型?

[英]How can I filter the column type of the table using two buttons?

I want to filter the type column, one button filters the aquatic and in the other all except the aquatic我想过滤类型列,一个按钮过滤水生,另一个按钮过滤水生

 <button type="button" onclick="Aquatic()">Aquatic</button> <button type="button" onclick="NotAquatic()" >everything but aquatic</button> <table class="table" > <thead> <tr> <th scope="col">Animal</th> <th scope="col">Type </th> </tr> </thead> <tbody id="" > <tr> <td>bat</td> <td>aerial</td> </tr> <tr> <td>Fish</td> <td>Aquatic</td> </tr> <tr> <td>Horse</td> <td>Land</td> </tr> <tr> <td>Shark</td> <td>Aquatic</td> </tr> <tr> <td>Elephant</td> <td>Land</td> </tr> <tr> <td>Whale</td> <td>Aquatic</td> </tr> <tr> <td>dove</td> <td>aerial</td> </tr> </tbody> </table>

Hopefully this helps point you in the right direction!希望这有助于为您指明正确的方向! have a filterTable function which grabs all the table rows, and removes any inline style changes previously added to display.有一个 filterTable 函数,它抓取所有表格行,并删除以前添加到显示的任何内联样式更改。 Then, I filter all the tr s by the innerHTML content not including the term, and I take all those tr s and set their display to none然后,我通过不包括术语的 innerHTML 内容过滤所有tr ,然后我采用所有这些tr并将它们的显示设置为无

 function filterTable(aquaShown) { const trs = [...document.querySelectorAll("tbody tr")]; trs.forEach(tr => tr.style.display = ""); const hiddenTrs = aquaShown ? trs.filter(tr => !tr.innerHTML.includes("Aquatic")) : trs.filter(tr => tr.innerHTML.includes("Aquatic")); hiddenTrs.forEach(tr => tr.style.display = "none"); } function aquatic() { filterTable(true); } function nonaquatic() { filterTable(false); }
 <button type="button" onclick="aquatic()">Aquatic</button> <button type="button" onclick="nonaquatic()">Non-Aquatic</button> <table class="table"> <thead> <tr> <th scope="col">Animal</th> <th scope="col">Type </th> </tr> </thead> <tbody id=""> <tr> <td>Fish</td> <td>Aquatic</td> </tr> <tr> <td>Horse</td> <td>Land</td> </tr> <tr> <td>Shark</td> <td>Aquatic</td> </tr> <tr> <td>Elephant</td> <td>Land</td> </tr> <tr> <td>Whale</td> <td>Aquatic</td> </tr> </tbody> </table>

EDIT refactored based on further clarification in comments编辑根据评论中的进一步说明进行重构

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

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