In MUI Data Table I’m using first column as checkboxes as sticky.enter image description here
when I scroll horizontal to left the data is overflowing need to fix this.
Please find below code
Table component
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import './customtable.css';
import { styled } from '@mui/material/styles';
import {
TableContainer,
Table,
TableHead,
TableRow,
TableCell,
TableBody,
Checkbox,
IconButton,
Button,
Paper,
TablePagination,
Switch,
FormGroup,
FormControlLabel,
} from '@mui/material';
import { makeStyles } from '@mui/styles';
import { CSVLink } from 'react-csv';
import jsPDF from 'jspdf';
import 'jspdf-autotable';
import image from '../../assets/images/Excel.png';
import pdf from '../../assets/images/pdf.png';
import { TextDecrease } from '@mui/icons-material';
import MUIDataTable from 'mui-datatables';
import { createTheme, ThemeProvider } from '@mui/material/styles';
const getMuiTheme = () =>
createTheme({
components: {
MUIDataTableToolbar: {
styleOverrides: {
root: {
minHeight: '50px',
display: 'flex',
},
actions: {
display: 'flex',
flex: 'inherit',
},
},
},
MUIDataTableBodyCell: {
styleOverrides: {
// padding: '10px',
root: {
border: 'none',
padding: 'none',
fontSize: '12px',
fontFamily: 'helvetica !important',
opacity:1,
},
simpleHeader: {
// padding: '10px',
},
simpleCell: {
// padding: '10px',
},
stackedHeader: {
verticalAlign: 'top',
},
stackedCommon: {
// padding: '10px',
},
stackedCommonAlways: {
// padding: '10px',
},
stackedParent: {
// padding: '10px',
},
stackedParentAlways: {
// padding: '10px',
},
cellStackedSmall: {
// padding: '10px',
},
responsiveStackedSmall: {
// padding: '10px',
},
responsiveStackedSmallParent: {
// padding: '10px',
},
},
},
MUIDataTableSelectCell: {
styleOverrides: {
root: {
opacity:1,
borderBottom: 'none',
},
headerCell: {
borderBottom: '1px solid #d9d9d9',
},
},
},
MUIDataTableHeadCell: {
styleOverrides: {
root: {
// borderBottom: 'none !important',
opacity:1,
borderTop: '1px solid #d9d9d9',
fontSize: '14px !important',
fontFamily: 'helvetica !important',
fontWeight: 'bold !important',
padding: '10px 16px 10px 16px',
// borderRight: 'none',
},
toolButton: {
fontSize: '14px !important',
fontFamily: 'helvetica !important',
fontWeight: 'bold !important',
},
},
},
MUIDataTableHeadRow: {
styleOverrides: {
root: {
opacity:1,
// borderBottom: 'none !important',
borderTop: '1px solid #d9d9d9',
fontSize: '14px !important',
fontFamily: 'helvetica !important',
fontWeight: 'bold !important',
// borderRight: 'none',
},
},
},
MUIDataTableFooter: {
styleOverrides: {
root: {
opacity:1,
borderTop: 'solid 1px #dee2e6',
},
},
},
MuiTableCell: {
styleOverrides: {
root: {
zIndex:1,
opacity:1,
color: "#000000"
},
},
},
},
});
const useStyles = makeStyles({
root: {
width: '100%',
fontSize: '12px',
fontWeight: 400,
fontStyle: 'helvetica',
textAlign: 'center',
opacity:1
},
container: {
opacity:1,
zIndex:1
},
});
const AntSwitch = styled(Switch)(({ theme }) => ({
width: 28,
height: 16,
padding: 0,
marginRight: 5,
display: 'flex',
'&:active': {
'& .MuiSwitch-thumb': {
width: 15,
},
'& .MuiSwitch-switchBase.Mui-checked': {
transform: 'translateX(9px)',
},
},
'& .MuiSwitch-switchBase': {
padding: 2,
'&.Mui-checked': {
transform: 'translateX(12px)',
color: '#fff',
opacity:1,
'& + .MuiSwitch-track': {
opacity: 1,
backgroundColor: theme.palette.mode === 'dark' ? '#177ddc' : '#9D1D27',
},
},
},
'& .MuiSwitch-thumb': {
boxShadow: '0 2px 4px 0 rgb(0 35 11 / 20%)',
width: 12,
height: 12,
borderRadius: 6,
transition: theme.transitions.create(['width'], {
duration: 200,
}),
},
'& .MuiSwitch-track': {
borderRadius: 16 / 2,
opacity: 1,
backgroundColor:
theme.palette.mode === 'dark'
? 'rgba(255,255,255,.35)'
: 'rgba(0,0,0,.25)',
boxSizing: 'border-box',
},
}));
const CustomTable = (props) => {
const classes = useStyles();
const {
data,
columns,
docTitle,
isReports,
handleShowMoreColumns,
showMoreColumns,
} = props;
const [page, setPage] = useState(0); // add state for current page
const [rowsPerPage, setRowsPerPage] = useState(5); // add state for rows per page
const [selectedRows, setSelectedRows] = useState([]);
useEffect(() => {
}, [selectedRows]);
const handleSelectAllClick = (event) => {
if (event.target.checked) {
const newSelectedRows = data.map((row) => row.id);
setSelectedRows(newSelectedRows);
} else {
setSelectedRows([]);
}
};
const handleClick = (event, rowId) => {
const selectedIndex = selectedRows.indexOf(rowId);
let newSelectedRows = [];
if (selectedIndex === -1) {
newSelectedRows = [...selectedRows, rowId];
} else if (selectedIndex > -1) {
newSelectedRows = [
...selectedRows.slice(0, selectedIndex),
...selectedRows.slice(selectedIndex + 1),
];
}
setSelectedRows(newSelectedRows);
};
const isSelected = (rowId) => selectedRows.indexOf(rowId) !== -1;
const selectedData = data.filter((row, index) =>
selectedRows.includes(index)
);
const handleChangePage = (event, newPage) => {
// function to handle page changes
setPage(newPage);
};
const handleChangeRowsPerPage = (event) => {
// function to handle rows per page changes
setRowsPerPage(parseInt(event.target.value, 10));
setPage(0);
};
const handleExportPDF = () => {
const unit = 'pt';
const pageSize = jsPDF.getPageSize('landscape', 'pt');
const orientation = 'landscape';
const marginLeft = 40;
const doc = new jsPDF(orientation, unit, [pageSize.width, pageSize.height]);
const headers = columns.map((column) => column.label);
const data = [];
selectedData.forEach((row) => {
const rowData = [];
columns.forEach((column) => {
rowData.push(row[column.name]);
});
data.push(rowData);
});
let totalPages = 1;
const content = {
startY: 50,
head: [headers],
body: data,
didParseCell: function (data) {
data.cell.styles.cellPadding = 5;
data.cell.styles.fontSize = 10;
data.cell.styles.overflow = 'linebreak';
data.cell.styles.cellHeight = 'auto';
},
columnStyles: {
// Set the width of each column here
// For example, this sets the width of the first column to 80pt
0: { cellWidth: 80 },
},
// Add a hook to check the height of the table on each page
didDrawPage: function (data) {
const { settings, table } = data;
const height = table.height + settings.margin.top;
if (totalPages === 1 && height > pageSize.height / 2) {
// Set the startY position to half the page height
content.startY = pageSize.height / 2;
// Add a new page and continue the table
doc.addPage();
// Increment the total page count
totalPages++;
// Draw the table again from the start row
this.startRowIndex = 0;
this.drawTable({ startRowIndex: this.startRowIndex });
} else if (totalPages > 1) {
// Increment the total page count
totalPages++;
// Draw the table again from the start row
this.startRowIndex = 0;
this.drawTable({ startRowIndex: this.startRowIndex });
}
},
};
doc.text(docTitle || 'Report', marginLeft, 40);
doc.autoTable(content);
doc.save(fileNamePdf);
};
const headers = columns.map((column) => {
return { label: column.label, name: column.name, key: column.name };
});
const options = {
filter: true,
resizableColumns: false,
print: false,
download: false,
rowHover: false,
pagination: true,
selectableRowsHeader: true,
// tableBodyHeight:'320px',
tableBodyMaxHeight: '320px',
selectableRows: 'multiple',
rowsPerPage: 5,
rowsPerPageOptions: [5, 10, 20, 50, 100],
filterType: 'textFiled',
responsive: 'vertical',
onRowSelectionChange: (rowsSelectedData, allRows, rowsSelected) => {
setSelectedRows(rowsSelected);
},
selectToolbarPlacement: 'none',
onRowsDelete: (rowsDeleted, newData) => {
return false;
},
customToolbar: () => {
return (
<div style={{ display: 'flex', justifyContent: 'end' }}>
<Button
// variant="contained"
// color="primary"
type="button"
style={{
float: 'right',
borderRadius: '12px',
paddingTop: '3px',
paddingBottom: '3px',
}}
>
{/* Export */}
<CSVLink
data={selectedData}
headers={headers}
filename={fileNameCSV}
>
<img
src={image}
width='30px'
height='30px'
style={{ marginLeft: '10px' }}
/>
</CSVLink>
<img
src={pdf}
width='20px'
height='20px'
onClick={handleExportPDF}
style={{ marginLeft: '10px' }}
/>
</Button>
</div>
);
},
};
let fileDateTime = new Date().toJSON().slice(0, 10);
let fileName = 'Report_' + fileDateTime; //+".csv";
let fileNameCSV = fileName + '.csv';
let fileNamePdf = fileName + '.pdf';
return (
<Paper className={classes.root}>
<TableContainer className={classes.container}>
<ThemeProvider theme={getMuiTheme()}>
<MUIDataTable className={classes.container}
// title='Search Results'
// components={{
// TableFooter: CustomFilterList,
// }}
data={data}
columns={headers}
options={options}
// components={components}
/>
</ThemeProvider>
</TableContainer>
</Paper>
);
};
export default CustomTable;
I don’t want to see data overflows inside checkboxes when I scroll horizontal left.
