How-to

Website

Web app

Page through results

A list of thousands of rows is too long to ask for at once. To show it a page at a time with Previous and Next buttons, keep the page number in state and splice it into the address:

Needs: a server that answers /api/rows?page=…&per=… with a list of rows. It claims /: paste it into a new project, or replace the starter's src/pages/Home.wf with it.

wf

type Row { id: String, name: String } type RowPage { items: [Row], pages: Number } page Rows(path: "/", title: "Rows", description: "Every row, twenty at a time.") { state page = 1 state perPage = 20 resource rows: RowPage = http.get("/api/rows?page={page}&per={perPage}") Heading("Rows").h1 match rows { .loading { Spinner } .failed(e) { Text(e.message).danger } .loaded(r) { for row in r.items by row.id { Text(row.name) } Row(gap: .sm, align: .center) { Button("Previous", disabled: page == 1) { on click { page = page - 1 } } Text("Page {page} of {r.pages}") Button("Next", disabled: page >= r.pages) { on click { page = page + 1 } } } } } }

How it works

The page shows the first twenty rows with Previous greyed out and "Page 1 of …" between the buttons. Next swaps in the next twenty and counts up; on the last page Next is greyed out instead.

  • page and perPage are states, and the resource splices both into its address.
  • The server answers with the page's items and the number of pages. The .loaded arm lists the items and draws the buttons.
  • Previous and Next change page. A change of page changes the address, so the resource asks again (Fetch again when state changes).
  • Each button is disabled at its end of the list, so page stays between 1 and r.pages.

To add each page below the ones already shown instead of replacing them, see Load more as the reader asks.