React material table automatic page size
The solution that worked for me is the folling (material-table docs):
<MaterialTable minRows={10}
localization={{
toolbar: {
searchPlaceholder: "Buscar",
searchTooltip: "Buscar "
},
pagination:{
labelRowsSelect:"Registros",
labelRowsPerPage:"Filas por pagina"
},
body: {
deleteTooltip: "Eliminar",
emptyDataSourceMessage: "No existen registros"
}
}}
title="Listado de registros"
columns={state.columns}
data={state.data}
actions={[
{
icon: 'add',
tooltip: 'Agregar',
isFreeAction: true,
onClick: props.addRegister
}
]}
options={{
pageSize: 10,
pageSizeOptions: [5, 10, 20, 30 ,50, 75, 100 ],
toolbar: true,
paging: true
}}
components={{
Pagination: props => (
<TablePagination
{...props}
labelRowsPerPage={<div style={{fontSize: 14}}>{props.labelRowsPerPage}</div>}
labelDisplayedRows={row => <div style={{fontSize: 14}}>{props.labelDisplayedRows(row)}</div>}
SelectProps={{
style:{
fontSize: 14
}
}}
/>
)
}}
>
</MaterialTable>
I too had the same requirement. So I found a solution, by using AutoSizer from the 'react-virtualized-auto-sizer' package. It goes well with the 'material-table' package.
Sample Code:
import AutoSizer from 'react-virtualized-auto-sizer';
export default function Monitor() {
const columns = [...];
const data = [..];
return (
<AutoSizer>
{({ height, width }) => {
console.log(`Height: ${height} | Width: ${width}`);
const pageSize = Math.floor((height - 192) / 48);
console.log(`Page Size: ${pageSize}`);
return (
<div style={{ height: `${height}px`, width: `${width}px`, overflowY: 'auto' }}>
<MaterialTable
columns={columns}
data={data}
options={{
pageSize: pageSize,
pageSizeOptions: [],
toolbar: true,
paging: true
}}
icons={tableIcons}
></MaterialTable>
</div>
);
}}
</AutoSizer>
);
}