-
Notifications
You must be signed in to change notification settings - Fork 0
Custom Pagination
📝 Generated from
docs/features/custom-pagination.md. Edit it there; changes made in the wiki are overwritten.
This guide shows how to use Ant Design (or any other UI library) components with the DataGrid.
Slots are typed React.ComponentType<Record<string, unknown>>, so the component takes Record<string, unknown> and narrows it to the props it reads (see Slots API).
import { Pagination, Select } from 'antd';
import { DataGrid } from '@opencorestack/opengridx';
type PaginationSlotProps = {
page: number;
pageSize: number;
rowCount: number;
pageSizeOptions: number[];
onPageChange: (page: number) => void;
onPageSizeChange: (pageSize: number) => void;
};
function AntdPaginationComponent(props: Record<string, unknown>) {
const {
page,
pageSize,
rowCount,
pageSizeOptions,
onPageChange,
onPageSizeChange
} = props as PaginationSlotProps;
// Client-side, `page` is already clamped to the last page.
const firstRowIndex = page * pageSize;
const lastRowIndex = Math.min(firstRowIndex + pageSize, rowCount);
// Show the page size in use even when it is not one of pageSizeOptions, as the built-in pager does.
const sizes = pageSizeOptions.includes(pageSize)
? pageSizeOptions
: [...pageSizeOptions, pageSize].sort((a, b) => a - b);
return (
<div style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'flex-end',
gap: '16px',
padding: '12px 16px',
borderTop: '1px solid #f0f0f0'
}}>
{/* Ant Design Select for page size */}
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
<span>Rows per page:</span>
<Select
value={pageSize}
onChange={onPageSizeChange}
options={sizes.map(size => ({
value: size,
label: size
}))}
style={{ width: 80 }}
/>
</div>
{/* Row count */}
<span>{rowCount === 0 ? 0 : firstRowIndex + 1}–{lastRowIndex} of {rowCount}</span>
{/* Ant Design Pagination */}
<Pagination
current={page + 1}
total={rowCount}
pageSize={pageSize}
onChange={(page) => onPageChange(page - 1)}
showSizeChanger={false}
simple
/>
</div>
);
}
// Usage in your app
function MyApp() {
return (
<DataGrid
rows={rows}
columns={columns}
pagination
slots={{
pagination: AntdPaginationComponent
}}
slotProps={{
pagination: {
// Any custom props for your component
}
}}
/>
);
}import { Select, MenuItem } from '@mui/material';
// PaginationSlotProps: the type alias from the example above
function MuiPaginationComponent(props: Record<string, unknown>) {
const { pageSize, pageSizeOptions, onPageSizeChange } = props as PaginationSlotProps;
return (
<div>
<Select
value={pageSize}
onChange={(e) => onPageSizeChange(Number(e.target.value))}
>
{pageSizeOptions.map(size => (
<MenuItem key={size} value={size}>
{size}
</MenuItem>
))}
</Select>
</div>
);
}Your custom pagination component will receive these props:
-
page: Current page index (0-based). With client pagination it is clamped to the last page: when the rows shrink below the current page, the grid shows the last page and reports the corrected page once throughonPaginationModelChange. -
pageSize: Current page size. It may be a value missing frompageSizeOptions(the built-in pager then lists it as an extra option). -
rowCount: Number of rows being paged: the filtered, unpinned rows (pinned rows show on every page), or the server total withpaginationMode="server". -
pageSizeOptions: Array of available page sizes (default[10, 25, 50, 100]). The default page size is 100, or the first option when 100 is not offered. -
onPageChange(newPage: number): Callback to change page -
onPageSizeChange(newPageSize: number): Callback to change page size (also goes back to page 0) -
localeText:{ paginationRowsPerPage, paginationOf, paginationPage }, passed only when the grid'slocaleTextprop is set. The built-in pager usespaginationRowsPerPageas the select's label and accessible name. - Any additional props from
slotProps.pagination
The slot is rendered only while pagination is in effect: pagination is set, row grouping is off and paginationMode is not "infinite". A slots.footer replaces it. List view uses it too.
<DataGrid
rows={rows}
columns={columns}
slots={{
pagination: CustomPaginationComponent,
noRowsOverlay: CustomNoRowsComponent,
loadingOverlay: CustomLoadingComponent,
footer: CustomFooterComponent
}}
slotProps={{
pagination: { /* custom props */ },
noRowsOverlay: { /* custom props */ },
loadingOverlay: { /* custom props */ },
footer: { /* custom props */ }
}}
/>See demo/examples/CustomPagination/CustomPagination.tsx for a complete working example.
OpenGridX 3.2.2 · MIT · This wiki is generated from docs/ on every push to main. To fix a page, open a PR against the source file.
Start here
Components
- DataGrid
- Header
- Row
- Cell
- Toolbar
- Pagination
- Filter Panel
- Tooltip
- Column Visibility
- Column Grouping
- Column Resizing
- Empty State
- Error Overlay
- Aggregation Footer
Features
- Virtualization
- Filtering & Search
- Sorting & Pagination
- Custom Pagination
- Editing & Reordering
- Row Selection
- Clipboard
- Pinning
- State Persistence
- Aggregation & Pivot
- Tree Data & Grouping
- Cell Spanning
- Master-Detail
- Keyboard & Accessibility
- List View
- Infinite Scroll
- Data Source
- Loading States
- Toolbar Customization
- Export (CSV, Excel, JSON, Print)
- PDF Export
Customization
Upgrading
Contributing
- Contributing
- Testing
- Roadmap
- DataGrid orchestration
- GridRowMeta
- useGridControlledState
- useGridRowPipeline
- useGridColumns
- useGridVirtualization
- useGridVisibleRows
- useGridScrollSync
- useGridStateSnapshot