VtTag 标签
标签列(纯展示):把行里的值画成彩色胶囊。不给 options 时值本身就是标签文案;给了 options 就是一份「值 → 文案 + 配色」的字典(不是候选集)。
示例
微应用尚未挂载。
源码
点击查看源码
ts
import { VirtTable, type VirtTableColumn } from '@virt-table/vanilla';
import { VtTag } from '@virt-table/vanilla/components';
/** 值 → 文案 + 配色的**字典**(不是候选集:VtTag 不参与选择) */
const STATUS = [
{ value: 'run', label: '运行中', color: '#10b981' },
{ value: 'queued', label: '排队', color: '#f59e0b' },
{ value: 'done', label: '已完成', color: '#2563eb' },
{ value: 'failed', label: '失败', color: '#ef4444' },
];
/** 语义色版本:不写死色值,跟着主题走(`--vt-color-success` 等) */
const RESULT = [
{ value: 'pass', label: '通过', variant: 'success' as const },
{ value: 'warn', label: '告警', variant: 'warning' as const },
{ value: 'fail', label: '不通过', variant: 'danger' as const },
];
const LEVELS = ['青铜', '白银', '黄金', '铂金', '钻石'];
export function bootstrapTableVtTag(root: HTMLElement): () => void {
const rowCount = 5000;
const columns: VirtTableColumn[] = [
{ key: 'id', title: 'ID', width: 70, align: 'center' },
{ key: 'name', title: '任务', width: 110 },
// ① 字典:行里存 code,显示文案 + 逐项自定义色
VtTag({ key: 'status', title: '状态(字典)', width: 150, options: STATUS }),
// ② 字典 + 语义色:颜色跟主题,不写死
VtTag({ key: 'result', title: '结果(语义色)', width: 140, options: RESULT }),
// ③ 不给 options:值本身就是标签文案
VtTag({ key: 'level', title: '等级(值即文案)', width: 150 }),
// ④ 按行动态上色:优先级最高,压过列级 variant
VtTag({
key: 'level',
title: '等级(按行上色)',
width: 150,
color: (row) => ((row as { levelIndex: number }).levelIndex >= 3 ? '#a855f7' : undefined),
}),
// ⑤ 空值:不画空胶囊,走 placeholder
VtTag({ key: 'owner', title: '负责人(可空)', width: 130, placeholder: '—' }),
];
const list = Array.from({ length: rowCount }, (_, i) => ({
id: i,
name: `任务 ${i}`,
status: STATUS[i % STATUS.length]!.value,
result: RESULT[i % RESULT.length]!.value,
level: LEVELS[i % LEVELS.length],
levelIndex: i % LEVELS.length,
owner: i % 4 === 0 ? '' : `成员 ${i % 7}`,
}));
root.innerHTML = `
<div id="status" class="status-text"></div>
<div style="width:900px;height:600px;" class="demo-container" id="vtContainer"></div>
`;
const container = root.querySelector('#vtContainer') as HTMLElement;
const statusEl = root.querySelector('#status') as HTMLElement;
const table = new VirtTable(container, {
list,
columns,
itemKey: 'id',
estimatedSize: 40,
buffer: 4,
border: true,
});
statusEl.textContent =
'VtTag:纯展示,没有 renderEditor —— 它没有候选集,值本身就是内容,' +
'配色能按行算。要「从候选集里选、选中的画成胶囊」用 VtSelect({ display: "tag" }):' +
'胶囊外观两者共用同一个工厂,长相必然一致,差别只在能不能选。';
return () => {
table.destroy();
root.innerHTML = '';
};
}说明
它不参与选择,因此不提供 renderEditor,也不需要任何插件。空值不画空胶囊,走 placeholder。
配色优先级只有一处实现,所以「纯展示的标签」和「选出来的标签」必然长得一样、跟主题也只跟一次:
options[].color > options[].variant > 列级 color > 列级 variant > 默认主色要「从候选集里选」用 VtSelect —— 「长什么样」是参数,不该是组件名:
| 要做的事 | 怎么写 |
|---|---|
| 只读的标签列 | VtTag({ key, options? }) |
| 可选的标签列(单值) | VtSelect({ key, options, display: 'tag' }) |
| 可选的标签列(多值) | VtSelect({ key, options, display: 'tag', multiple: true }) |
胶囊外观两者共用同一个工厂(createTagChip),差别只在能不能选。更多写法(多值折叠、只读多值、 运行时换形态)见 VtSelect · 常见配方。
相关 API
| 字段 | 作用 |
|---|---|
options | 「值 → 文案 + 配色」字典,可不给 |
variant / color | 列级配色,可写成 (row, rowIndex) => … |
全量选项表见 单元格组件;公共选项 (
placeholder/disabled/clearable/onChange)、asCell()、三类交互模型见 同页上方;列的通用字段见 列配置。