function PaginationDemo() {
const showFirstLastPageButtonsStates = {
all: true,
first: { last: false },
last: { first: false },
none: false,
};
const [
{
paging,
emptyRowsWhenPaging,
paginationType,
numberOfPagesAround,
paginationPosition,
showFirstLastPageButtonsState,
},
setOptions,
] = React.useState({
paging: true,
emptyRowsWhenPaging: true,
paginationType: "normal",
paginationPosition: "bottom",
numberOfPagesAround: 1,
showFirstLastPageButtonsState: "all",
});
const showFirstLastPageButtons =
showFirstLastPageButtonsStates[showFirstLastPageButtonsState];
return (
<>
<FormGroup>
<FormControlLabel
control={
<Switch
checked={paging}
onChange={(e) => {
setOptions((prev) => ({ ...prev, paging: e.target.checked }));
}}
/>
}
label="Paging"
/>
<FormControlLabel
control={
<Switch
checked={emptyRowsWhenPaging}
onChange={(e) => {
setOptions((prev) => ({
...prev,
emptyRowsWhenPaging: e.target.checked,
}));
}}
/>
}
label="Empty Rows When Paging"
/>
<Button
variant="contained"
sx={{ mb: 1 }}
onClick={() => {
setOptions(({ paginationPosition: prevPos, ...prev }) => ({
...prev,
paginationPosition:
prevPos === "bottom"
? "top"
: prevPos === "top"
? "both"
: "bottom",
}));
}}
>
Pagination Position: {paginationPosition}
</Button>
<Button
variant="contained"
sx={{ mb: 1 }}
onClick={() => {
setOptions(({ paginationType: prevType, ...prev }) => ({
...prev,
paginationType: prevType === "normal" ? "stepped" : "normal",
}));
}}
>
Pagination Type: {paginationType}
</Button>
{paginationType === "stepped" ? (
<Button
variant="contained"
sx={{ mb: 1 }}
onClick={() => {
setOptions(({ numberOfPagesAround: prevPages, ...prev }) => ({
...prev,
numberOfPagesAround: Math.max((prevPages + 1) % 11, 1),
}));
}}
>
Number Of Pages Around: {numberOfPagesAround}
</Button>
) : null}
</FormGroup>
<FormControl>
<FormLabel id="demo-controlled-radio-buttons-group">
Show First/Last Buttons
</FormLabel>
<RadioGroup
row={true}
name="controlled-radio-buttons-group"
value={showFirstLastPageButtonsState}
onChange={(e) => {
setOptions((prev) => ({
...prev,
showFirstLastPageButtonsState: e.target.value,
}));
}}
>
<FormControlLabel value="all" control={<Radio />} label="All" />
<FormControlLabel value="none" control={<Radio />} label="None" />
<FormControlLabel
value="first"
control={<Radio />}
label="First only"
/>
<FormControlLabel
value="last"
control={<Radio />}
label="Last Only"
/>
</RadioGroup>
</FormControl>
<MaterialTable
data={Array(50)
.fill()
.map((_, i) => ({ id: i, name: `name ${i + 1}` }))}
columns={DEMO_COLS}
options={{
paging,
emptyRowsWhenPaging,
paginationType,
numberOfPagesAround,
paginationPosition,
showFirstLastPageButtons,
}}
/>
</>
);
}