Lab 7: Infinite Scroll
Last updated
Last updated
In Lab 5 we improved the perceived responsiveness of our application by lazy loading the user table.
Even without a simulated delay, the response was still quite slow as we transferred a 4.1 MB HTML table:
In this lab we will fix this by using pagination with the infinite scroll mechanism, only loading new users when the user wants to see them.
First, we add a paginated retrieval method to the UserService:
We also add two new render methods to the ListComponent
to allow us to combine ViewContextLists.
We then create a InfiniteLoadCompoent.java
ViewComponent in the de.tschuehly.easy.spring.auth.user.management.table.infinite
package.
In the render method, we define a nextPage
parameter that we can access in the ViewContext:
Then we will create a InfiniteLoadComponent.jte
template in the same package:
(1): We define an hx-get
attribute that requests the GET_USER_TABLE
endpoint with the nextPage
viewContext property as URI variable.
(2): With hx-trigger="intersect once"
we can tell htmx to create the request only once when the element intersects with the browser viewport.
(3): With hx-swap="outerHTML"
we tell htmx to swap the whole <tr>
element.
We now need to change the UserController
GET_USER_TABLE
endpoint to accept a page @PathVariable
by both adjusting the constant and method
Next, we need to adjust the UserTableComponent
to accept a page parameter:
(1): In the method, we first retrieve the page of users with the userService.getPage
method.
(2): We then stream through the list of users and call the userRowComponent::render
to get a ViewContext list
(3): Next we will call the listComponent.render
method and pass the userRowList
(4): We also pass theinfiniteLoadComponent.render
method with the currentPage
increased by one.
(5): When the user is on the first page we want to render the full UserTableComponent
(6): But for each following page, we want to render the userRowList
and the infiniteLoadComponent
without the whole table structure by just passing the tableBody
variable-
(7): We now need to change the UserTableContext
to have a userTableBody
parameter
Now in the UserTableComponent
, we need to render the userTableBody
ViewContext property in the <tbody>
element.
We need to adjust the UserMangement.jte
template to pass in 0 as the parameter.
If we restart the application and navigate to http://localhost:8080 we can see that the infinite scroll is working again!
Lab-7 Checkpoint 1
If you are stuck you can resume at this checkpoint with:
git checkout tags/lab-7-checkpoint-1 -b lab-7-c1