【问题标题】:React material table automatic page sizeReact 材料表自动页面大小
【发布时间】:2020-01-07 06:40:20
【问题描述】:

我正在使用 React + Material Table。

我的问题

  • 有没有办法根据页面上的可用空间动态设置pageSize
  • 如果没有 API 可以做到这一点 - 如何从组件设计的角度更好地解决这个问题?

我想达到什么目的?

材料表中显示的行数应取决于屏幕尺寸。根据您的屏幕尺寸,页面看起来不会相似(例如,在笔记本电脑设备上看起来不错,但在 25 英寸显示器上会有很多空间可以按行填充)。

我已经做了什么?

  • 我搜索了official documentation,但找不到解决方案。
  • 我也在寻找开发人员的帖子和其他 SO 主题 - 仍然没有结果。

当然,可以构建一个脚本,根据容器大小和行大小进行一些简单的计算,以填充尽可能多的行,但我想避免这种解决方案并使用开箱即用的东西如果可能的话。

【问题讨论】:

标签: reactjs material-ui material-design material-table


【解决方案1】:

我也有同样的要求。所以我找到了一个解决方案,通过使用 'react-virtualized-auto-sizer' 包中的 AutoSizer。它与 'material-table' 包配合得很好。

示例代码:

    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>
    );
    }

【讨论】:

    【解决方案2】:

    对我有用的解决方案是以下 (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>
    

    【讨论】:

    • 你能解释一下吗?我不明白这如何解决 OP 的问题。
    • 当然,在“选项”中你可以设置pageSize属性,10的值显示第一次加载表中的这一行,如果你想设置20,那么第一次加载应该显示20表中的行
    • 感谢您的解释,但我认为 OP 的问题是他不提前知道表格应该显示多少行。如果窗口高度很大,那么 OP 希望表格自动调整大小并用尽可能多的行填充窗口。
    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2020-05-10
    • 2018-06-17
    • 2020-06-10
    • 2020-04-20
    相关资源
    最近更新 更多