You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 08:39:02