简体   繁体   中英

Filter table using values in specific column

I am trying to filter a table using javascript to only show rows which have more then 5 seeders. Here is a link to a JSFiddle with an example of the table.

http://jsfiddle.net/xQB4Z/566/

Example of one row:

<td align=left width=500><NOBR><a href="download.php?id=1241487&SSL=1"><img style="position: relative; top: 2px;" src="themes/classic/pic/download.gif.pagespeed.ce.6SI31hDpjb.gif" border="0"></a>&nbsp;<a class="index" href="details.php?id=1241487&amp;hit=1">CENTOS 4</a> (<b><font color="red">NEW!</font></b>)</NOBR><br/><font size=1 color='666666'><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Uploaded 13 m, 44 seconds after pre</i></font></td>
<td align="right"><b><a href="details.php?id=1241487&amp;hit=1&amp;filelist=1">78</a></b></td>
<td align="right">0</td>
<td align=center><nobr>2016-08-17<br/>23:23:27</nobr></td>
<td align=center>----</td>
<td align=center>6.64<br/>GB</td>
<td align=center>----</td>
<td align="right"><span class="green">2</span></td>
<td align="right"><span class="green">2</span></td>
</tr>

Here's something simple to get you started:

var rows = document.getElementById("yourTableId").querySelectorAll("tr");

for (var i = 1; i < rows.length; i++) { // start at row 1 to skip header
  if (+rows[i].cells[8].textContent <= 5)
    rows[i].style.display = "none";
}

Demo: http://jsfiddle.net/xQB4Z/568/

Note: I gave your table an id. You could instead use document.querySelector("table") to just get the first table on the page.

You can do something like this:

Array.from(document.querySelectorAll("tr")).forEach(function(v, k){
    var seeders = v.querySelectorAll("td")[8].textContent;
    if(seeders < 5){
        v.style.display = "none";
    }
});

What this will do is to check the content of 9th column in each row and if the value in this column is smaller then 5, hide the row.

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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