CxJS

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>
NamePhoneCity
Alice Johnson 1555-1000New York
Bob Smith 2555-1001Los Angeles
Carol White 3555-1002Chicago
David Brown 4555-1003Houston
Eva Green 5555-1004Phoenix
Alice Johnson 6555-1005New York
Bob Smith 7555-1006Los Angeles
Carol White 8555-1007Chicago
David Brown 9555-1008Houston
Eva Green 10555-1009Phoenix
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:

  1. Remote sorting - Set remoteSort on the Grid to prevent client-side sorting
  2. Filter inputs in headers - Use header2 with children for filter fields
  3. Reactive triggers - Use addTrigger to fetch data when parameters change
  4. Lock column widths - Use lockColumnWidths to 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

PropertyTypeDescription
sortersProp<Sorter[]>Bound to the current sorting state.
remoteSortbooleanDisables client-side sorting when true.
lockColumnWidthsbooleanLocks column widths to prevent layout shifts during data updates.

Pagination Configuration

PropertyTypeDescription
pageProp<number>Currently selected page (1-indexed).
pageCountProp<number>Total number of pages.
lengthnumberNumber of page buttons to display. Default is 5.