DocsGuides

Guides

Pagination Guide

How to use, customize, or disable pagination features in Shadcn Table

Client-side pagination is enabled by default in Shadcn Table. There are a number of ways to customize pagination, turn off pagination, or completely replace the built-in pagination with your own manual or server-side pagination logic.

Example

Here is the full Remote Data example showing off server-side filtering, pagination, and sorting.

Storybook

Relevant Table Options

Prop NameTypeDescription
enablePaginationboolean
manualPaginationbooleanEnables manual pagination. If this option is set to true, the table will not automatically paginate rows using getPaginationRowModel() and instead will expect you to manually paginate the rows before passing them to the table. This is useful if you are doing server-side pagination and aggregation.
onPaginationChangeOnChangeFn<PaginationState>If this function is provided, it will be called when the pagination state changes and you will be expected to manage the state yourself. You can pass the managed state back to the table via the tableOptions.state.pagination option.
pageCountnumberWhen manually controlling pagination, you should supply a total pageCount value to the table if you know it. If you do not know how many pages there are, you can set this to -1.
paginateExpandedRowsbooleanIf true expanded rows will be paginated along with the rest of the table (which means expanded rows may span multiple pages). If false expanded rows will not be considered for pagination (which means expanded rows will always render on their parents page. This also means more rows will be rendered than the set page size)
paginationDisplayMode'compact' | 'custom' | 'pages'The pagination controls. 'pages' (the default) shows the rows per page select, the row range, numbered page buttons and previous and next; 'compact' shows the rows per page select, the row range and the first, previous, next and last buttons; 'custom' renders nothing.
positionPagination'bottom' | 'top' | 'both'
rowCountnumber
stPaginationPropsPartial<PaginationProps> | ({ table }) => Partial<PaginationProps>

Relevant State Options

State OptionTypeDescription
pagination{ pageIndex: number, pageSize: number }

Disable Pagination

If you simply want to disable pagination, you can set the enablePagination table option to false. This will both hide the pagination controls and disable the pagination functionality.

If you only want to disable the pagination logic, but still want to show and use the pagination controls, take a look down below at the Manual Pagination docs.

const table = useShadcnTable({
  columns,
  data,
  enablePagination: false,
  enableBottomToolbar: false, //hide the bottom toolbar as well if you want
});

Customize Pagination

Pagination State

If you want to change the rows per page value or the initial page index, you can do that through either the initialState or state table option.

//using initialState if you do not need to manage the pagination state yourself
const table = useShadcnTable({
  columns,
  data,
  initialState: { pagination: { pageSize: 25, pageIndex: 2 } }, //customize the default page size and page index
});
//using state if you want to manage the pagination state yourself
const [pagination, setPagination] = useState({
  pageIndex: 0,
  pageSize: 5, //customize the default page size
});

const table = useShadcnTable({
  columns,
  data,
  onPaginationChange: setPagination, //hoist pagination state to your state when it changes internally
  state: { pagination }, //pass the pagination state to the table
});

Note: Do not pass pagination state to both initialState and state at the same time. The state will overwrite the initialState values.

Customize Pagination Behavior

There are a few table options that you can use to customize the pagination behavior. The first one is autoResetPageIndex. This table option is true by default, and causes a table to automatically reset the table back to the first page whenever sorting, filtering, or grouping occurs. This makes sense for most use cases, but if you want to disable this behavior, you can set this table option to false.

Next there is paginateExpandedRows, which works in conjunction expanding features. This table option is true by default, and forces the table to still only render the same number of rows per page that is set as the page size, even as sub-rows become expanded. However, this does cause expanded rows to sometimes not be on the same page as their parent row, so you can turn this off to keep sub rows with their parent row on the same page.

Customize Pagination Components

You can customize the pagination component with the stPaginationProps table option to change things like the rowsPerPageOptions or whether or not to show the first and last page buttons, and more.

const table = useShadcnTable({
  columns,
  data,
  stPaginationProps: {
    rowsPerPageOptions: [5, 10, 20],
    showFirstButton: false,
    showLastButton: false,
  },
});

Pagination Display Modes

By default (paginationDisplayMode: 'pages'), the bottom toolbar shows the shadcn/ui Pagination component on the right: the rows per page select, the shown row range such as "1-10 of 100", numbered page buttons, and previous and next buttons. The bottom toolbar sits inside the table frame, separated from the last row by a plain 1 px hairline, and its left side stays empty unless you fill it with renderBottomToolbarCustomActions.

If you prefer the more compact, traditional data table pagination (the rows per page select, the shown row range, and first, previous, next and last buttons), set the paginationDisplayMode table option to 'compact'. Set 'custom' to render no pagination controls and place your own.

const table = useShadcnTable({
  columns,
  data,
  paginationDisplayMode: 'compact', //the compact rows per page, row range and arrow buttons UI
});
Storybook

Manual or Server-Side Pagination

Manual Pagination

The default pagination features are client-side. This means you have to have all of your data fetched and stored in the table all at once. This may not be ideal for large datasets, but do not worry, Shadcn Table supports server-side pagination.

When the manualPagination table option is set to true, Shadcn Table will assume that the data that is passed to the table already has had the pagination logic applied. Usually you would do this in your back-end logic.

Override Page Count and Row Count

If you are using manual pagination, the default page count and row count in the ST Pagination component will be incorrect, as it is only derived from the number of rows provided in the client-side data table option. Luckily, you can override these values and set your own page count or row count in the pageCount and rowCount table options.

const table = useShadcnTable({
  columns,
  data,
  manualPagination: true,
  rowCount: data.meta.totalDBRowCount, //you can tell the pagination how many rows there are in your back-end data
});

Manage Pagination State

For either client-side or server-side pagination, you may want to have access to the pagination state yourself. You can do this like so with state:

//store pagination state in your own state
const [pagination, setPagination] = useState({
  pageIndex: 0,
  pageSize: 5, //customize the default page size
});

useEffect(() => {
  //do something when the pagination state changes
}, [pagination.pageIndex, pagination.pageSize]);

const table = useShadcnTable({
  columns,
  data,
  onPaginationChange: setPagination, //hoist pagination state to your state when it changes internally
  state: { pagination }, //pass the pagination state to the table
});

return <ShadcnTable table={table} />;

Alternatively, if all you care about is customizing the initial pagination state and do not need to react to its changes, like customizing the default page size or the page index, you can do that like so with initialState:

const table = useShadcnTable({
  columns,
  data,
  initialState: { pagination: { pageSize: 25, pageIndex: 2 } },
});

View Extra Storybook Examples

On this page