简体   繁体   English

如何计算过滤后显示的表行数?

[英]How can I count how many table rows are shown after filtering?

How can I count how many table rows are shown after filtering? 如何计算过滤后显示的表行数?

I've tried running this script, but it shows total rows, not currently shown rows: 我尝试运行此脚本,但它显示的是总行,而不是当前显示的行:

function CountRows() {
    var rowCount = 0;
    var table = document.getElementById("filter_table");
    var rows = table.getElementsByTagName("tr")
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].getElementsByTagName("td").length > 0) {
            rowCount++;
        }
    }
    var message = "Showing: " + rowCount;
    alert(message);
}

Location: http://kissbandstree.com/count_rows.js 位置: http//kissbandstree.com/count_rows.js

There is no error message. 没有错误信息。

This is the filter script: http://kissbandstree.com/multifilter.js 这是过滤器脚本: http : //kissbandstree.com/multifilter.js

This is the test page: http://kissbandstree.com/artists2.php 这是测试页: http : //kissbandstree.com/artists2.php

I want to show how many rows are showing after a filter has been applied. 我想显示应用过滤器后显示多少行。

This is the relevant PHP / HTML part: 这是相关的PHP / HTML部分:

<?php
// Fetch all filenames in the kiss_artists directory
$artists = glob('kiss_artists/*.txt');
?>

<table class="standard">
  <tr>
    <td>
        <?php echo"Items in table: " . count($artists) . ". Showing: XXX. Table is sortable. Artists names are links to simple individual pages. Date is when the entry was modified."; ?>
        <input id="clickMe" type="button" value="clickme" onclick="CountRows();">
    </td>
  </tr>
</table>

You should be able to skip the hidden rows by adding a check for visibility to your loop: 通过添加循环可见性检查,您应该能够跳过隐藏的行:

for (var i = 0; i < rows.length; i++) {
    if (rows[i].style.display == 'none') continue;
    if (rows[i].getElementsByTagName("td").length > 0) {
        rowCount++;
    }
}
// for the header row
rowCount--;

Since there is a header row you need to subtract one from your count. 由于存在标题行,因此您需要从计数中减去一个。

You can check all the count of visible rows only.. because by seeing that page, you are hiding rows that are not relevant to the filter. 您只能检查所有可见行的计数。.因为看到该页面,您正在隐藏与过滤器不相关的行。

I have to minus one count from total just because of header row was calculated too. 我也不得不从总数中减去一个数,这仅仅是因为标头行也被计算出来了。

Also, Since you use jQuery in that page, this should be the simplest solution 另外,由于您在该页面中使用了jQuery,因此这应该是最简单的解决方案

function countRows(){
      let rowCount = $('#filter_table tr:visible').length - 1;
      var message = "Showing: " + rowCount;
      alert(message);
    }

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

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