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
+21 -8

No files matched your search

+18 -5
View File
@@ -14,6 +14,8 @@ import {
Typography,
Stack,
} 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 RightArrowDouble from "../../assets/icons/right-arrow-double.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 theme = useTheme();
const dispatch = useDispatch();
const { table } = useSelector((state) => state.ui);
let rowsPerPage = table.rowsPerPage;
const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(rows);
useEffect(() => {
setPage(0);
@@ -154,7 +158,7 @@ const BasicTable = ({ data, paginated, reversed, rows = 5 }) => {
};
const handleChangeRowsPerPage = (event) => {
setRowsPerPage(parseInt(event.target.value, 10));
dispatch(setRowsPerPage(parseInt(event.target.value, 10)));
setPage(0);
};
@@ -211,7 +215,12 @@ const BasicTable = ({ data, paginated, reversed, rows = 5 }) => {
</TableBody>
</Table>
</TableContainer>
{paginated && <Stack direction="row" alignItems="center" justifyContent="space-between">
{paginated && (
<Stack
direction="row"
alignItems="center"
justifyContent="space-between"
>
<Typography sx={{ opacity: 0.7 }}>
Showing {getRange()} of {data.rows.length} monitor(s)
</Typography>
@@ -226,7 +235,10 @@ const BasicTable = ({ data, paginated, reversed, rows = 5 }) => {
ActionsComponent={TablePaginationActions}
labelRowsPerPage="Rows per page"
labelDisplayedRows={({ page, count }) =>
`Page ${page + 1} of ${Math.max(0, Math.ceil(count / rowsPerPage))}`
`Page ${page + 1} of ${Math.max(
0,
Math.ceil(count / rowsPerPage)
)}`
}
slotProps={{
select: {
@@ -254,7 +266,8 @@ const BasicTable = ({ data, paginated, reversed, rows = 5 }) => {
}}
sx={{ mt: theme.gap.medium }}
/>
</Stack>}
</Stack>
)}
</>
);
};
+3 -3
View File
@@ -13,8 +13,8 @@ const uiSlice = createSlice({
name: "ui",
initialState,
reducers: {
setTablePreferences: (state, action) => {
state.table = { ...action };
setRowsPerPage: (state, action) => {
state.table.rowsPerPage = action.payload;
},
toggleSidebar: (state) => {
state.sidebar.collapsed = !state.sidebar.collapsed;
@@ -23,4 +23,4 @@ const uiSlice = createSlice({
});
export default uiSlice.reducer;
export const { setTablePreferences, toggleSidebar } = uiSlice.actions;
export const { setRowsPerPage, toggleSidebar } = uiSlice.actions;