Pagination
import { Grid, Pagination } from 'cx/widgets'; Copied Grid is commonly used with server-side pagination, sorting, and filtering. The Pagination widget provides navigation controls, while reactive triggers manage data fetching.
<div controller={PageController}>
<Grid
records={m.records}
style="width: 100%"
mod="bordered"
lockColumnWidths
sorters={m.sorters}
remoteSort
columns={[
{
field: "fullName",
sortable: true,
header1: "Name",
header2: {
allowSorting: false,
style: "font-weight: 400; padding: 2px",
children: (
<TextField
value={m.filter.name}
reactOn="enter blur"
style="width: 100%"
placeholder="Filter..."
/>
),
},
},
{
field: "phone",
header1: "Phone",
header2: {
style: "font-weight: 400; padding: 2px",
children: (
<TextField
value={m.filter.phone}
reactOn="enter blur"
style="width: 100%"
placeholder="Filter..."
/>
),
},
},
{
field: "city",
sortable: true,
header1: "City",
header2: {
allowSorting: false,
style: "font-weight: 400; padding: 2px",
children: (
<TextField
value={m.filter.city}
reactOn="enter blur"
style="width: 100%"
placeholder="Filter..."
/>
),
},
},
]}
/>
<div style="margin-top: 16px; display: flex; justify-content: space-between; align-items: center">
<Pagination page={m.page} pageCount={m.pageCount} />
<Select value={m.pageSize}>
<option value={5}>5 per page</option>
<option value={10}>10 per page</option>
<option value={20}>20 per page</option>
<option value={50}>50 per page</option>
</Select>
</div>
</div> | Name | Phone | City |
|---|---|---|
| Alice Johnson 1 | 555-1000 | New York |
| Bob Smith 2 | 555-1001 | Los Angeles |
| Carol White 3 | 555-1002 | Chicago |
| David Brown 4 | 555-1003 | Houston |
| Eva Green 5 | 555-1004 | Phoenix |
| Alice Johnson 6 | 555-1005 | New York |
| Bob Smith 7 | 555-1006 | Los Angeles |
| Carol White 8 | 555-1007 | Chicago |
| David Brown 9 | 555-1008 | Houston |
| Eva Green 10 | 555-1009 | Phoenix |
1
2
3
4
5
Click column headers to sort. Type in the filter fields and press Enter to filter. Use the pagination controls to navigate between pages.
How It Works
Server-side pagination requires coordination between the Grid, Pagination widget, and a controller that fetches data. The key concepts are:
- Remote sorting - Set
remoteSorton the Grid to prevent client-side sorting - Filter inputs in headers - Use
header2withchildrenfor filter fields - Reactive triggers - Use
addTriggerto fetch data when parameters change - Lock column widths - Use
lockColumnWidthsto prevent layout shifts during data loads
// Reset to page 1 when filters or page size change
this.addTrigger("resetPage", [m.pageSize, m.sorters, m.filter], () => {
this.store.set(m.page, 1);
}, true);
// Fetch data when any pagination parameter changes
this.addTrigger("fetchData", [m.pageSize, m.page, m.sorters, m.filter],
(size, page, sorters, filter) => {
// Server call would go here
// Update m.records and m.pageCount with results
},
true
);
Grid Configuration
| Property | Type | Description |
|---|---|---|
sorters | Prop<Sorter[]> | Bound to the current sorting state. |
remoteSort | boolean | Disables client-side sorting when true. |
lockColumnWidths | boolean | Locks column widths to prevent layout shifts during data updates. |
Pagination Configuration
| Property | Type | Description |
|---|---|---|
page | Prop<number> | Currently selected page (1-indexed). |
pageCount | Prop<number> | Total number of pages. |
length | number | Number of page buttons to display. Default is 5. |