Add pagination (#7958)

This commit is contained in:
Matthias Mair 2024-08-22 09:01:25 +02:00 committed by GitHub
parent 1dff94db75
commit 58f60d18b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 5 deletions

View File

@ -36,6 +36,8 @@ export type TableState = {
setRecordCount: (count: number) => void; setRecordCount: (count: number) => void;
page: number; page: number;
setPage: (page: number) => void; setPage: (page: number) => void;
pageSize: number;
setPageSize: (pageSize: number) => void;
records: any[]; records: any[];
setRecords: (records: any[]) => void; setRecords: (records: any[]) => void;
updateRecord: (record: any) => void; updateRecord: (record: any) => void;
@ -99,6 +101,7 @@ export function useTable(tableName: string): TableState {
// Pagination data // Pagination data
const [page, setPage] = useState<number>(1); const [page, setPage] = useState<number>(1);
const [pageSize, setPageSize] = useState<number>(25);
// A list of hidden columns, saved to local storage // A list of hidden columns, saved to local storage
const [hiddenColumns, setHiddenColumns] = useLocalStorage<string[]>({ const [hiddenColumns, setHiddenColumns] = useLocalStorage<string[]>({
@ -158,6 +161,8 @@ export function useTable(tableName: string): TableState {
setRecordCount, setRecordCount,
page, page,
setPage, setPage,
pageSize,
setPageSize,
records, records,
setRecords, setRecords,
updateRecord, updateRecord,

View File

@ -55,6 +55,7 @@ import { RowAction, RowActions } from './RowActions';
import { TableSearchInput } from './Search'; import { TableSearchInput } from './Search';
const defaultPageSize: number = 25; const defaultPageSize: number = 25;
const PAGE_SIZES = [10, 15, 20, 25, 50, 100, 500];
/** /**
* Set of optional properties which can be passed to an InvenTreeTable component * Set of optional properties which can be passed to an InvenTreeTable component
@ -74,7 +75,6 @@ const defaultPageSize: number = 25;
* @param enableRefresh : boolean - Enable refresh actions * @param enableRefresh : boolean - Enable refresh actions
* @param enableColumnSwitching : boolean - Enable column switching * @param enableColumnSwitching : boolean - Enable column switching
* @param enableColumnCaching : boolean - Enable caching of column names via API * @param enableColumnCaching : boolean - Enable caching of column names via API
* @param pageSize : number - Number of records per page
* @param barcodeActions : any[] - List of barcode actions * @param barcodeActions : any[] - List of barcode actions
* @param tableFilters : TableFilter[] - List of custom filters * @param tableFilters : TableFilter[] - List of custom filters
* @param tableActions : any[] - List of custom action groups * @param tableActions : any[] - List of custom action groups
@ -100,7 +100,6 @@ export type InvenTreeTableProps<T = any> = {
enableLabels?: boolean; enableLabels?: boolean;
enableReports?: boolean; enableReports?: boolean;
afterBulkDelete?: () => void; afterBulkDelete?: () => void;
pageSize?: number;
barcodeActions?: React.ReactNode[]; barcodeActions?: React.ReactNode[];
tableFilters?: TableFilter[]; tableFilters?: TableFilter[];
tableActions?: React.ReactNode[]; tableActions?: React.ReactNode[];
@ -129,7 +128,6 @@ const defaultInvenTreeTableProps: InvenTreeTableProps = {
enableRefresh: true, enableRefresh: true,
enableSearch: true, enableSearch: true,
enableSelection: false, enableSelection: false,
pageSize: defaultPageSize,
defaultSortColumn: '', defaultSortColumn: '',
barcodeActions: [], barcodeActions: [],
tableFilters: [], tableFilters: [],
@ -360,7 +358,8 @@ export function InvenTreeTable<T = any>({
// Pagination // Pagination
if (tableProps.enablePagination && paginate) { if (tableProps.enablePagination && paginate) {
let pageSize = tableProps.pageSize ?? defaultPageSize; let pageSize = tableState.pageSize ?? defaultPageSize;
if (pageSize != tableState.pageSize) tableState.setPageSize(pageSize);
queryParams.limit = pageSize; queryParams.limit = pageSize;
queryParams.offset = (tableState.page - 1) * pageSize; queryParams.offset = (tableState.page - 1) * pageSize;
} }
@ -588,6 +587,12 @@ export function InvenTreeTable<T = any>({
[props.onRowClick, props.onCellClick] [props.onRowClick, props.onCellClick]
); );
// pagination refresth table if pageSize changes
function updatePageSize(newData: number) {
tableState.setPageSize(newData);
tableState.refreshTable();
}
return ( return (
<> <>
{deleteRecords.modal} {deleteRecords.modal}
@ -705,7 +710,7 @@ export function InvenTreeTable<T = any>({
idAccessor={tableProps.idAccessor} idAccessor={tableProps.idAccessor}
minHeight={300} minHeight={300}
totalRecords={tableState.recordCount} totalRecords={tableState.recordCount}
recordsPerPage={tableProps.pageSize ?? defaultPageSize} recordsPerPage={tableState.pageSize}
page={tableState.page} page={tableState.page}
onPageChange={tableState.setPage} onPageChange={tableState.setPage}
sortStatus={sortStatus} sortStatus={sortStatus}
@ -733,6 +738,8 @@ export function InvenTreeTable<T = any>({
overflow: 'hidden' overflow: 'hidden'
}) })
}} }}
recordsPerPageOptions={PAGE_SIZES}
onRecordsPerPageChange={updatePageSize}
/> />
</Box> </Box>
</Stack> </Stack>