File size: 1,413 Bytes
b5bffcc
 
 
 
 
 
1108474
 
3b5501b
b5bffcc
 
 
 
 
 
 
 
1108474
b5bffcc
 
 
 
 
 
3b5501b
b5bffcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from fasthtml.common import Div, Table, Tbody, Td, Th, Thead, Tr, fast_app

app, rt = fast_app()


# fmt: off
@app.get
def page():
    return Div(cls="container")(
        Table(
            Thead(Tr(Th("Name"), Th("ID"))),
            Tbody(load_contacts(page=1)),
        ),
    )
# fmt: on


@app.get
def load_contacts(page: int, limit: int = 5):
    rows = [Tr(Td("Smith"), Td((page - 1) * limit + i)) for i in range(1, limit)]
    return *rows, make_last_row(page, limit)


def make_last_row(page, limit):
    return Tr(hx_trigger="revealed", hx_swap="afterend", hx_get=load_contacts.rt(page=page + 1))(
        Td("Smith"),
        Td(page * limit),
    )


DESC = "Demonstrates infinite scrolling of a page"
DOC = """
The infinite scroll pattern provides a way to load content dynamically on user scrolling action.

Let’s focus on the final row (or the last element of your content):
::make_last_row load_contacts::
This last element contains a listener which, when scrolled into view, will trigger a request. The result is then appended after it. The last element of the results will itself contain the listener to load the next page of results, and so on.

<blockquote><ins>revealed</ins> triggered when an element is scrolled into the viewport (also useful for lazy-loading). If you are using overflow in css like overflow-y: scroll you should use intersect once instead of revealed.</blockquote>
"""