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

使用react-table的表格在移动端无响应,overflow-x-auto不生效

解决React-Table移动端自适应横向滚动问题
  • 给表格添加最小宽度,触发横向滚动:
    修改table的className,加入min-w-full,让表格宽度至少占满容器,当内容宽度超出容器时,父容器的overflow-x-auto才会生效显示滚动条。

  • 覆盖daisyui表格的固定布局:
    DaisyUI的table类默认可能设置了table-layout: fixed,会强制单元格均分宽度、截断内容,导致表格无法随内容扩展。可以通过内联样式或自定义类覆盖该属性:

    <table {...getTableProps()} className="table table-xs md:table-md lg:table-lg mt-10 border-collapse min-w-full" style={{ tableLayout: 'auto' }}>
    
  • 确保父容器无额外宽度限制:
    包裹表格的div仅保留overflow-x-auto,避免添加固定宽度类(如w-96),保证容器能自适应移动端视口宽度。

修改后的核心代码片段:

<div className="overflow-x-auto">
  <table {...getTableProps()} className="table table-xs md:table-md lg:table-lg mt-10 border-collapse min-w-full" style={{ tableLayout: 'auto' }}>
    <thead>
      {headerGroups.map((headerGroup, idx) => (
        <tr key={idx} {...headerGroup.getHeaderGroupProps()}>
          {headerGroup.headers.map((column, idx) => (
            <th key={idx} {...column.getHeaderProps()}>
              {column.render("Header")}
            </th>
          ))}
        </tr>
      ))}
    </thead>
    <tbody {...getTableBodyProps()}>
      {page.map((row, idx) => {
        prepareRow(row);
        return (
          <tr key={idx} {...row.getRowProps()}>
            {row.cells.map((cell, idx) => (
              <td key={idx} {...cell.getCellProps()}>
                {cell.render("Cell")}
              </td>
            ))}
          </tr>
        );
      })}
    </tbody>
  </table>

  {/* 分页组件可保留在容器内,也可根据需求移至外部 */}
  <div className="mt-12 flex flex-col space-y-1 md:flex-row justify-between items-center">
    {/* 分页内容不变 */}
  </div>
</div>

原理说明:overflow-x-auto仅在子元素宽度超过父容器时显示滚动条,min-w-full保证表格在内容较少时占满容器,内容过多时自动扩展;table-layout: auto让表格根据内容自动计算列宽,避免固定布局导致的内容截断。

内容的提问来源于stack exchange,提问作者Ayan Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 00:52:54