React 功能列(序号 / 勾选 / 单选)
col.type 声明的列不画 row[key],而是由表格接管这一格:序号列画行号、勾选列画多选框、 单选列画单选钮。三种同表并存互不干扰 —— 勾选是「选了哪些行」,单选是「当前是哪一行」, 两套状态各自独立。
示例
微应用尚未挂载。
源码
点击查看源码
tsx
import React from 'react';
import { VirtTableReact, type ReactTableColumn, type VirtTableRef } from '@virt-table/react';
import { faker } from '@faker-js/faker';
/** 序号 / 勾选 / 单选 三种 `col.type` 同表并存(勾选与单选各占一列,状态互不干扰) */
interface Row extends Record<string, unknown> {
id: number;
name: string;
city: string;
dept: string;
}
const ROW_COUNT = 1000;
const columns: ReactTableColumn<Row>[] = [
{ key: '__index', title: '#', width: 60, type: 'index' as const, align: 'center' as const },
{ key: '__checkbox', title: '', width: 50, type: 'checkbox' as const, align: 'center' as const },
{ key: '__radio', title: '当前', width: 60, type: 'radio' as const, align: 'center' as const },
{ key: 'id', title: 'ID', width: 80 },
{ key: 'name', title: '姓名', width: 160 },
{ key: 'city', title: '城市', width: 160 },
{ key: 'dept', title: '部门', width: 200 },
];
const list: Row[] = Array.from({ length: ROW_COUNT }, (_, i) => ({
id: i + 1,
name: faker.person.fullName(),
city: faker.location.city(),
dept: faker.commerce.department(),
}));
export default function FeatureColumnsTable() {
const tableRef = React.useRef<VirtTableRef>(null);
const [status, setStatus] = React.useState(
`序号 / 勾选 / 单选 三种功能列同表:${ROW_COUNT} 行`,
);
return (
<div className="demo-wrapper">
<h3 className="demo-title">React 功能列</h3>
<div className="virt-table-controls">
<button
type="button"
onClick={() => {
const rows = (tableRef.current?.getCheckedRows() ?? []) as Row[];
setStatus(
rows.length
? `勾选了 ${rows.length} 行:${rows.map((r) => r.id).join(', ')}`
: '没有勾选任何行',
);
}}
>
获取勾选行
</button>
<button
type="button"
onClick={() => {
tableRef.current?.clearCheckedRows();
setStatus('已清空勾选');
}}
>
清空勾选
</button>
<button
type="button"
onClick={() => {
const r = tableRef.current?.getSelectedRadio() as Row | null | undefined;
setStatus(r ? `当前行:${r.name}(id=${r.id})` : '没有当前行');
}}
>
获取当前行
</button>
<button
type="button"
onClick={() => {
tableRef.current?.setSelectedRadio(null);
setStatus('已清除当前行');
}}
>
清除当前行
</button>
</div>
<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,
onCheckChange: (checked, row) => {
const count = (tableRef.current?.getCheckedRows() ?? []).length;
setStatus(
`勾选:行 ${(row as Row).id} ${checked ? '选中' : '取消'},共 ${count} 行`,
);
},
onCheckAll: (checked) => {
const count = (tableRef.current?.getCheckedRows() ?? []).length;
setStatus(checked ? `全选 ${count} 行` : '取消全选');
},
onRadioChange: (row) => {
const r = row as Row;
setStatus(`单选:当前行 ${r.name}(id=${r.id})`);
},
}}
/>
</div>
</div>
);
}说明
ts
const columns: VirtTableColumn<Row>[] = [
{ key: '__index', title: '#', width: 60, type: 'index', align: 'center' },
{ key: '__checkbox', title: '', width: 50, type: 'checkbox', align: 'center' },
{ key: '__radio', title: '当前', width: 60, type: 'radio', align: 'center' },
{ key: 'name', title: '姓名', width: 160 },
];功能列的 key 只要在列里唯一即可(不必对应任何数据字段)。
读写选择态
| 想做的事 | 勾选(多选) | 单选 |
|---|---|---|
| 监听变化 | onCheckChange(checked, row) / onCheckAll(checked) | onRadioChange(row) |
| 读当前值 | getCheckedRows() | getSelectedRadio() |
| 写入 | setCheckedRows(keys) | setSelectedRadio(rowKey | null) |
| 清空 | clearCheckedRows() | setSelectedRadio(null) |
六种功能列
type | 画什么 | 另需 |
|---|---|---|
index | 行号,自动递增 | — |
checkbox | 多选框(表头带全选) | — |
radio | 单选钮 | — |
expand | 展开箭头 | renderExpandRow,见 展开行 |
drag | 行拖拽手柄 | vtRowDrag 插件,见 行拖拽排序 |
tree | 缩进 + 折叠箭头 | 行数据含 children,见 树形 |
独占单元格,以及 tree 那个例外
功能列属于接管层:配在 index / checkbox / radio / expand / drag 上的 render / renderEditor / cellType 会被忽略并在控制台告警(早期版本是静默吞掉)。 整列不要这个控件,就别配 type —— 那种「配了 render 又配 type」是配置写错了。
tree 是唯一例外,它其实属于容器层:只画缩进与折叠箭头,内容照常走 render → cellType → 默认取值那条链,因此也吃得到 textOverflow、align。
分层的全貌见 渲染粒度。
单元格级可以穿透
「就这一行不给勾选」「这一行的序号换成图标」是真实需求,在此之前没有任何出口。 所以单元格级配置可以穿透功能列(列级不行,理由同上):
ts
tableRef.current?.setCellRender('42', '__checkbox', { render: () => '🔒' });穿透只改 UI,不改选择态
勾选框没画出来,但该行仍在选择作用域里 —— 表头「全选」照样会选中它。要真正禁止选中, 在 onCheckChange / onCheckAll 里自己挡。
相关 API
| 字段 / 方法 | 作用 | 详见 |
|---|---|---|
col.type | 功能列类型,六种取值 | 列配置 · 功能列 |
onCheckChange / onCheckAll | 勾选变化回调 | 表格配置 |
getCheckedRows / setCheckedRows / clearCheckedRows | 勾选态读写 | 实例方法 |
onRadioChange / getSelectedRadio / setSelectedRadio | 单选态读写 | 数据与远程 · 单选行 |
setCellRender | 单元格级配置,可穿透功能列 | 单元格渲染 |
上面的字段在 React 端原样透传(实例方法通过组件
ref调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 JSX、ref转发了哪些方法 —— 见 React API。