Provided optional pagination for the table.
This commit is contained in:
1 parent
c9a3f54a65
commit
9a99f89b89
1 file changed
+23
-18
@@ -1,3 +1,4 @@
|
|||||||
|
import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { styled, useTheme } from '@mui/material/styles';
|
import { styled, useTheme } from '@mui/material/styles';
|
||||||
import Table from '@mui/material/Table';
|
import Table from '@mui/material/Table';
|
||||||
@@ -8,12 +9,8 @@ import TableHead from '@mui/material/TableHead';
|
|||||||
import TableRow from '@mui/material/TableRow';
|
import TableRow from '@mui/material/TableRow';
|
||||||
import Paper from '@mui/material/Paper';
|
import Paper from '@mui/material/Paper';
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
|
import Pagination from '../../Components/Pagination';
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a styled TableCell component.
|
|
||||||
* @param {object} theme - The theme object.
|
|
||||||
* @returns {JSX.Element} The styled TableCell component.
|
|
||||||
*/
|
|
||||||
const StyledTableCell = styled(TableCell)(({ theme }) => ({
|
const StyledTableCell = styled(TableCell)(({ theme }) => ({
|
||||||
[`&.${tableCellClasses.head}`]: {
|
[`&.${tableCellClasses.head}`]: {
|
||||||
backgroundColor: theme.palette.action.hover,
|
backgroundColor: theme.palette.action.hover,
|
||||||
@@ -24,11 +21,6 @@ const StyledTableCell = styled(TableCell)(({ theme }) => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a styled TableRow component.
|
|
||||||
* @param {object} theme - The theme object.
|
|
||||||
* @returns {JSX.Element} The styled TableRow component.
|
|
||||||
*/
|
|
||||||
const StyledTableRow = styled(TableRow)(({ theme }) => ({
|
const StyledTableRow = styled(TableRow)(({ theme }) => ({
|
||||||
'&:nth-of-type(odd)': {
|
'&:nth-of-type(odd)': {
|
||||||
backgroundColor: theme.palette.action.hover,
|
backgroundColor: theme.palette.action.hover,
|
||||||
@@ -41,15 +33,21 @@ const StyledTableRow = styled(TableRow)(({ theme }) => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
/**
|
const itemsPerPage = 5; // Number of items per page
|
||||||
* Customized table component displaying incident history.
|
|
||||||
* @param {object} props - The props object.
|
|
||||||
* @param {array} props.data - The array of data objects containing JSX elements for name, and strings for date and message.
|
|
||||||
* @param {array} props.columns - The array of column headers and alignments.
|
|
||||||
* @returns {JSX.Element} The JSX element representing the customized table.
|
|
||||||
*/
|
|
||||||
export default function StatusTable({ data, columns }) {
|
export default function StatusTable({ data, columns }) {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
const [currentPage, setCurrentPage] = React.useState(1);
|
||||||
|
|
||||||
|
const onPageChange = (page) => {
|
||||||
|
setCurrentPage(page);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Calculate total pages based on data length and items per page
|
||||||
|
const totalPages = Math.ceil(data.length / itemsPerPage);
|
||||||
|
|
||||||
|
// Slice data based on current page and items per page
|
||||||
|
const paginatedData = data.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
@@ -75,7 +73,7 @@ export default function StatusTable({ data, columns }) {
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{data.map((row, rowIndex) => (
|
{paginatedData.map((row, rowIndex) => (
|
||||||
<StyledTableRow key={rowIndex}>
|
<StyledTableRow key={rowIndex}>
|
||||||
{columns.map((column, colIndex) => (
|
{columns.map((column, colIndex) => (
|
||||||
<StyledTableCell key={colIndex} align={column.align || 'left'}>
|
<StyledTableCell key={colIndex} align={column.align || 'left'}>
|
||||||
@@ -93,6 +91,13 @@ export default function StatusTable({ data, columns }) {
|
|||||||
</TableBody>
|
</TableBody>
|
||||||
</Table>
|
</Table>
|
||||||
</TableContainer>
|
</TableContainer>
|
||||||
|
{totalPages > 1 && (
|
||||||
|
<Pagination
|
||||||
|
page={currentPage}
|
||||||
|
count={totalPages}
|
||||||
|
onPageChange={onPageChange}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user