I am browsing HTML tables with pages that auto load via javascript & XMLHttpRequest
calls as the user scrolls. The number of records per page are user defined. The height of the rows can vary. All the code is vanilla javascript
and all solutions should be likewise.
I am tracking which row is crossing the middle of the table's viewable rows to determine what is the current page. This is used to display the current page number and to determine when to autoload the next page (think of how Acrobat displays the current page number and thumbnail as you scroll through a PDF). My process to do this:
- Use the intersection observer to observe
TR
rows that scroll in and out of the device's viewport. The triggeredtr
will either be at the top or bottom of the viewport. - Starting at the triggered row, I loop through the
tbody
'str
elements moving toward the middle until I find thetr
which intersects with it. I can calculate the current page from thetr
's customdata-rownum
attribute.
I discovered that the intersection observer's boundingClientRect
property is only updated and current for the triggered element, not the other observed row elements. Therefore I can't use it to calculate a row's position on the screen. I have to call getBoundingClientRect()
for each tr
to determine its position as I loop through the tbody
.
My question is how to measure the performance cost of calling getBoundingClientRect()
- perhaps 20-50 times per scroll - every time the observer triggers a new row being visible. Is there a way to objectively decide the impact?
Is it worth the effort performance-wise to find another solution? The only other solution I have in mind is to start with the triggered row and use the height
property of each tr
to calculate it's position on the screen using the triggered tr
as a starting point. But, in order to do this, I'd have to have a resizeObserver
set on all the rows to update the height property on resize, since they can be expanded by the user.
Lastly, is there a different direction that would allow me to track all rows that pass through the middle of the viewable tbody
that would be more efficient?