Added action to change rows per page

This commit is contained in:
Daniel Cojocea committed 2024-08-20 17:27:07 -04:00
1 parent c379348581
commit 1419b20591
2 files changed
+59 -46

No files matched your search

+56 -43
View File
@@ -14,6 +14,8 @@ import {
Typography, Typography,
Stack, Stack,
} from "@mui/material"; } from "@mui/material";
import { useDispatch, useSelector } from "react-redux";
import { setRowsPerPage } from "../../Features/UI/uiSlice";
import LeftArrowDouble from "../../assets/icons/left-arrow-double.svg?react"; import LeftArrowDouble from "../../assets/icons/left-arrow-double.svg?react";
import RightArrowDouble from "../../assets/icons/right-arrow-double.svg?react"; import RightArrowDouble from "../../assets/icons/right-arrow-double.svg?react";
import LeftArrow from "../../assets/icons/left-arrow.svg?react"; import LeftArrow from "../../assets/icons/left-arrow.svg?react";
@@ -142,8 +144,10 @@ const TablePaginationActions = (props) => {
const BasicTable = ({ data, paginated, reversed, rows = 5 }) => { const BasicTable = ({ data, paginated, reversed, rows = 5 }) => {
const theme = useTheme(); const theme = useTheme();
const dispatch = useDispatch();
const { table } = useSelector((state) => state.ui);
let rowsPerPage = table.rowsPerPage;
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(rows);
useEffect(() => { useEffect(() => {
setPage(0); setPage(0);
@@ -154,7 +158,7 @@ const BasicTable = ({ data, paginated, reversed, rows = 5 }) => {
}; };
const handleChangeRowsPerPage = (event) => { const handleChangeRowsPerPage = (event) => {
setRowsPerPage(parseInt(event.target.value, 10)); dispatch(setRowsPerPage(parseInt(event.target.value, 10)));
setPage(0); setPage(0);
}; };
@@ -211,50 +215,59 @@ const BasicTable = ({ data, paginated, reversed, rows = 5 }) => {
</TableBody> </TableBody>
</Table> </Table>
</TableContainer> </TableContainer>
{paginated && <Stack direction="row" alignItems="center" justifyContent="space-between"> {paginated && (
<Typography sx={{ opacity: 0.7 }}> <Stack
Showing {getRange()} of {data.rows.length} monitor(s) direction="row"
</Typography> alignItems="center"
<TablePagination justifyContent="space-between"
component="div" >
count={data.rows.length} <Typography sx={{ opacity: 0.7 }}>
page={page} Showing {getRange()} of {data.rows.length} monitor(s)
onPageChange={handleChangePage} </Typography>
rowsPerPage={rowsPerPage} <TablePagination
rowsPerPageOptions={[5, 10, 15, 25]} component="div"
onRowsPerPageChange={handleChangeRowsPerPage} count={data.rows.length}
ActionsComponent={TablePaginationActions} page={page}
labelRowsPerPage="Rows per page" onPageChange={handleChangePage}
labelDisplayedRows={({ page, count }) => rowsPerPage={rowsPerPage}
`Page ${page + 1} of ${Math.max(0, Math.ceil(count / rowsPerPage))}` rowsPerPageOptions={[5, 10, 15, 25]}
} onRowsPerPageChange={handleChangeRowsPerPage}
slotProps={{ ActionsComponent={TablePaginationActions}
select: { labelRowsPerPage="Rows per page"
MenuProps: { labelDisplayedRows={({ page, count }) =>
keepMounted: true, `Page ${page + 1} of ${Math.max(
PaperProps: { 0,
className: "pagination-dropdown", Math.ceil(count / rowsPerPage)
)}`
}
slotProps={{
select: {
MenuProps: {
keepMounted: true,
PaperProps: {
className: "pagination-dropdown",
},
transformOrigin: { vertical: "bottom", horizontal: "left" },
anchorOrigin: { vertical: "top", horizontal: "left" },
sx: { mt: "-4px" },
}, },
transformOrigin: { vertical: "bottom", horizontal: "left" }, inputProps: { id: "pagination-dropdown" },
anchorOrigin: { vertical: "top", horizontal: "left" }, IconComponent: SelectorVertical,
sx: { mt: "-4px" }, sx: {
}, ml: theme.gap.small,
inputProps: { id: "pagination-dropdown" }, mr: theme.gap.large,
IconComponent: SelectorVertical, minWidth: theme.gap.xl,
sx: { textAlign: "left",
ml: theme.gap.small, "&.Mui-focused > div": {
mr: theme.gap.large, backgroundColor: theme.palette.otherColors.white,
minWidth: theme.gap.xl, },
textAlign: "left",
"&.Mui-focused > div": {
backgroundColor: theme.palette.otherColors.white,
}, },
}, },
}, }}
}} sx={{ mt: theme.gap.medium }}
sx={{ mt: theme.gap.medium }} />
/> </Stack>
</Stack>} )}
</> </>
); );
}; };
+3 -3
View File
@@ -13,8 +13,8 @@ const uiSlice = createSlice({
name: "ui", name: "ui",
initialState, initialState,
reducers: { reducers: {
setTablePreferences: (state, action) => { setRowsPerPage: (state, action) => {
state.table = { ...action }; state.table.rowsPerPage = action.payload;
}, },
toggleSidebar: (state) => { toggleSidebar: (state) => {
state.sidebar.collapsed = !state.sidebar.collapsed; state.sidebar.collapsed = !state.sidebar.collapsed;
@@ -23,4 +23,4 @@ const uiSlice = createSlice({
}); });
export default uiSlice.reducer; export default uiSlice.reducer;
export const { setTablePreferences, toggleSidebar } = uiSlice.actions; export const { setRowsPerPage, toggleSidebar } = uiSlice.actions;