简体   繁体   English

如何计算HTML表中*可见*的行数?

[英]How to count the number of *visible* rows in an HTML table?

I'm meant to display a (read-only, no filtering, no lazy-loading) table in PrimeFaces (5.2), where the table should extend all the way down to the footer with "empty rows", should there not be enough rows. 我的意思是在PrimeFaces(5.2)中显示一个(只读,无过滤,无延迟加载)表,如果没有足够的空间,该表应一直扩展到带有“空行”的页脚行。 The footer has a fixed position, and so the table gets a scrollbar if there are too many rows. 页脚位置固定,因此如果行太多,表格将获得滚动条。 It seems the standard solution to getting empty rows is to return "fake rows" along with the result. 似乎获取空行的标准解决方案是返回“假行”和结果。 But I do not know how many to return, because the server does not know how many rows the client can see, so I return more then could be reasonably seen (100). 但是我不知道要返回多少行,因为服务器不知道客户端可以看到多少行,因此可以合理地看到返回的行数(100)。 My plan was then to remove the unneeded dummy rows with JavaScript, by checking their "visibility", but this proves to be an impossible task. 然后我的计划是通过检查JavaScript的“可见性”来删除不需要的虚拟行,但这被证明是不可能的。 No method of checking visibility I have seen so far work. 到目前为止,我没有看到检查可见性的方法。 My table is defined as follow: 我的表定义如下:

<p:dataTable id="tableId" var="orderView" value="#{orderTable.getPaddedOrderViews(100)}" 
          emptyMessage="EMPTY" scrollable="true" scrollHeight="550"
          resizableColumns="true" resizeMode="fit">
    ...

(getPaddedOrderViews(100) should return at least 100 rows, padding with dummy rows if needed) (getPaddedOrderViews(100)应该至少返回100行,如果需要,请使用虚拟行填充)

and the following code should find the visible/invisible rows (so I can change it to remove the invisible dummy rows later): 并且以下代码应找到可见/不可见的行(因此我可以对其进行更改以稍后删除不可见的虚拟行):

      function isVisible1(ele) {
          if (ele !== null &&
              ele !== undefined &&
              ele.clientWidth !== 0 &&
              ele.clientHeight !== 0) {
              var style = window.getComputedStyle(ele,null);
              return style !== null &&
                  style !== undefined &&
                  style.opacity !== 0 &&
                  style.visibility !== 'hidden' &&
                  style.display !== 'none';
          }
          return false;
      }
      function isVisible2(ele) {
          return ele !== null &&
              ele !== undefined &&
              ele.clientWidth !== 0 &&
              ele.clientHeight !== 0 &&
              ele.style !== null &&
              ele.style !== undefined &&
              ele.style.opacity !== 0 &&
              ele.style.visibility !== 'hidden' &&
              ele.style.display !== 'none';
      }
      function isVisible3(ele) {
          return ele.offsetWidth > 0 || ele.offsetHeight > 0;
      }
      function countVisibleRows() {
          var count = 0;
          var result1 = 0;
          var result2 = 0;
          var result3 = 0;
          var r;
          for (r in document.getElementById("tableId_data").rows) {
              count++;
              /*
              FAILS BECAUSE window.getComputedStyle(ele,null) says
              argument 1 is not an object
              if (isVisible1(r)) {
                  result1++;
              }*/
              if (isVisible2(r)) {
                  result2++;
              }
              if (isVisible3(r)) {
                  result3++;
              }
          }
          return [count,result1,result2,result3];
      }

        jQuery(document).ready(function () {
            jQuery(document).ready(function () {
                // twice in document.ready to execute after Primefaces callbacks
                try {
                    alert("ROWS: "+countVisibleRows());
                }
                catch(err) {
                    alert(err.message);
                }
            });
        });

But it displays "ROWS: [103,0,0,0]" for a table where I can see about 22 (non empty/dummy) rows (the number I was hoping to get), with more hidden. 但是它在一个表上显示“ ROWS:[103,0,0,0]”,在其中我可以看到约22(非空/虚拟)行(我希望获得的行数),其中还有更多隐藏的行。 Some are my dummy rows, but I cannot see any of them in this case. 有些是我的虚拟行,但在这种情况下我看不到其中的任何一行。 The 103 proves (IMHO), that I am correctly fetching the rows. 103证明(IMHO),我正确地获取了行。

isVisible1(ele) fails when calling window.getComputedStyle(ele,null). 调用window.getComputedStyle(ele,null)时,isVisible1(ele)失败。 isVisible2(ele) always returns false because no row has a style property, and isVisible3(ele) also always returns false. 因为没有行具有style属性,所以isVisible2(ele)总是返回false,并且isVisible3(ele)总是返回false。

So, why haven't the rows got any 'style', and how can I tell if they are visible without this 'style'? 那么,为什么行没有任何“样式”,又如何确定没有此“样式”的行是否可见?

Many questions/solutions regarding counting the visible rows where only for "filtered" tables, but that does not apply in my case. 关于仅对“已过滤”表计数可见行的许多问题/解决方案,但这不适用于我的情况。

If the number of visible rows can somehow be extracted from the table itself, rather then checking the rows, that would solve my problem too. 如果可以从表本身中提取可见行的数量,而不是检查行,那也将解决我的问题。

I based myself on this question to find out how to check visibility. 我根据这个问题来了解如何检查可见性。

If I understand properly you want to remove the overflow rows. 如果我理解正确,则要删除溢出行。

Since you are using jQuery can do this fairlry easily by comparing the offset of the rows to the offset of the bottom of the container 由于您使用的是jQuery,因此可以通过将行的偏移量与容器底部的偏移量进行比较来轻松实现此目的

var $cont = $('#container')
var contBottom = $cont.height() + $cont.offset().top;

$('tr').filter(function () {
    var $row = $(this),
        top = $row.offset().top,
        ht = $row.height();        
    return (top + ht) > contBottom;
}).remove()

DEMO 演示

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

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