Skip to content

React 可调整列宽 ​

为列设置 resizable: true 后,可拖动列边界调整宽度。

示例 ​

微应用尚未挂载。

源码 ​

点击查看源码
tsx
import React from 'react';
import { VirtTableReact, type ReactTableColumn, type VirtTableRef } from '@virt-table/react';
import { faker } from '@faker-js/faker';

const COL_COUNT = 10;
const ROW_COUNT = 200;

const columns: ReactTableColumn[] = Array.from({ length: COL_COUNT }, (_, i) => ({
  key: `extra_${i}`,
  title: `列 ${i}`,
  width: 150,
  resizable: true,
  minWidth: 80,
  maxWidth: 400,
}));

const list = Array.from({ length: ROW_COUNT }, (_, i) => {
  const row: Record<string, unknown> = { id: i };
  for (let c = 0; c < COL_COUNT; c++) {
    row[`extra_${c}`] = `${i}-${c}-${faker.lorem.word()}`;
  }
  return row;
});

export default function ResizeTable() {
  const tableRef = React.useRef<VirtTableRef>(null);
  const status = '列宽可拖拽(resizable: true)';
  return (
    <div className="demo-wrapper">
      <h3 className="demo-title">React 列宽调整</h3>
      <div className="virt-table-controls" />
      <div className="status-text">{status}</div>
      <div style={{ width: 800, height: 600 }} className="demo-container">
        <VirtTableReact
          ref={tableRef}
          columns={columns}
          options={{ list, itemKey: 'id', estimatedSize: 40, buffer: 4, border: true }}
        />
      </div>
    </div>
  );
}

相关 API ​

字段 / 方法作用详见
col.resizable允许拖拽调整该列列宽列配置
col.minWidth / maxWidth拖拽的上下界列配置
getState() / setState()把用户调过的列宽存下来状态持久化

全量字段见 表格配置 / 列配置 / 实例方法。

上面的字段在 React 端原样透传(实例方法通过组件 ref 调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 JSX、ref 转发了哪些方法 —— 见 React API。