如何为material-table的MaterialTable设置X轴横向滚动功能?
解决方案
你需要在MaterialTable组件的options配置项中新增两个参数:
tableLayout: 'fixed':固定表格布局规则,禁止列宽被自动压缩适配外层容器宽度overflowX: 'auto':当表格总宽度超出外层容器宽度时,自动展示横向滚动条
如果需要避免列内容被挤压,建议在columns的每一项配置中添加minWidth属性指定列的最小宽度,示例如下:
const columns = [ { title: '日志ID', field: 'log_id', minWidth: 120 }, { title: '操作账号', field: 'account', minWidth: 180 }, // 其余列按需配置minWidth即可 ]
修改后的完整组件代码如下:
<MaterialTable title="Members Account Logs" columns={columns} icons={tableIcons} data={data} options={{ headerStyle: { backgroundColor: 'black', color: 'white' }, rowStyle:{ backgroundColor: 'white', color: 'black' }, // 新增横向滚动相关配置 tableLayout: 'fixed', overflowX: 'auto' }} />
内容的提问来源于stack exchange,提问作者Beren
相关产品推荐
相关产品推荐

