功能列(序号 / 勾选 / 单选)
col.type 声明的列不画 row[key],而是由表格接管这一格:序号列画行号、勾选列画多选框、 单选列画单选钮。三种同表并存互不干扰 —— 勾选是「选了哪些行」,单选是「当前是哪一行」, 两套状态各自独立。
示例
微应用尚未挂载。
源码
点击查看源码
ts
import { faker } from '@faker-js/faker';
import { VirtTable, type VirtTableColumn } from '@virt-table/vanilla';
/**
* 功能列:序号 / 勾选 / 单选 三种 `col.type` 同表并存。
*
* 刻意放在一张表上 —— 它们是同一件事的三种形态(接管层独占单元格),而勾选与单选
* 各占一列、各有一套独立状态,同表并存正好说明「两者互不干扰」。
* 原先是三个示例(index-col / checkbox / radio),一页只能挂一个 PlaygroundHost,
* 所以合成这一个 demo。
*/
interface Row extends Record<string, unknown> {
id: number;
name: string;
city: string;
dept: string;
}
const ROW_COUNT = 1000;
export function bootstrapTableFeatureColumns(root: HTMLElement): () => void {
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: '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(),
}));
root.innerHTML = `
<div class="virt-table-controls">
<button class="virt-table-btn virt-table-btn-primary" id="btnChecked">获取勾选行</button>
<button class="virt-table-btn virt-table-btn-danger" id="btnClearCheck">清空勾选</button>
<button class="virt-table-btn virt-table-btn-primary" id="btnRadio">获取当前行</button>
<button class="virt-table-btn" id="btnClearRadio">清除当前行</button>
</div>
<div id="status" class="status-text" style="margin-bottom:8px;"></div>
<div style="width:800px;height:600px;" class="demo-container" id="tableContainer"></div>
`;
const container = root.querySelector('#tableContainer') as HTMLElement;
const status = root.querySelector('#status') as HTMLElement;
const setStatus = (text: string) => {
status.textContent = text;
};
const table = new VirtTable<Row>(container, {
list,
columns,
itemKey: 'id',
estimatedSize: 40,
buffer: 4,
border: true,
onCheckChange: (checked, row) => {
setStatus(
`勾选:行 ${row.id} ${checked ? '选中' : '取消'},共 ${table.getCheckedRows().length} 行`,
);
},
onCheckAll: (checked) => {
setStatus(checked ? `全选 ${table.getCheckedRows().length} 行` : '取消全选');
},
onRadioChange: (row) => {
setStatus(`单选:当前行 ${row.name}(id=${row.id})`);
},
});
const handlers: Array<[HTMLElement, () => void]> = [
[
root.querySelector('#btnChecked') as HTMLElement,
() => {
const rows = table.getCheckedRows();
setStatus(rows.length ? `勾选了 ${rows.length} 行:${rows.map((r) => r.id).join(', ')}` : '没有勾选任何行');
},
],
[
root.querySelector('#btnClearCheck') as HTMLElement,
() => {
table.clearCheckedRows();
setStatus('已清空勾选');
},
],
[
root.querySelector('#btnRadio') as HTMLElement,
() => {
const r = table.getSelectedRadio();
setStatus(r ? `当前行:${r.name}(id=${r.id})` : '没有当前行');
},
],
[
root.querySelector('#btnClearRadio') as HTMLElement,
() => {
table.setSelectedRadio(null);
setStatus('已清除当前行');
},
],
];
handlers.forEach(([el, fn]) => el.addEventListener('click', fn));
setStatus(`序号 / 勾选 / 单选 三种功能列同表:${ROW_COUNT} 行`);
return () => {
handlers.forEach(([el, fn]) => el.removeEventListener('click', fn));
table.destroy();
root.innerHTML = '';
};
}说明
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
table.setCellRender('42', '__checkbox', { render: () => '🔒' });穿透只改 UI,不改选择态
勾选框没画出来,但该行仍在选择作用域里 —— 表头「全选」照样会选中它。要真正禁止选中, 在 onCheckChange / onCheckAll 里自己挡。
相关 API
| 字段 / 方法 | 作用 | 详见 |
|---|---|---|
col.type | 功能列类型,六种取值 | 列配置 · 功能列 |
onCheckChange / onCheckAll | 勾选变化回调 | 表格配置 |
getCheckedRows / setCheckedRows / clearCheckedRows | 勾选态读写 | 实例方法 |
onRadioChange / getSelectedRadio / setSelectedRadio | 单选态读写 | 数据与远程 · 单选行 |
setCellRender | 单元格级配置,可穿透功能列 | 单元格渲染 |