Skip to content

Vue 功能列(序号 / 勾选 / 单选) ​

col.type 声明的列不画 row[key],而是由表格接管这一格:序号列画行号、勾选列画多选框、 单选列画单选钮。三种同表并存互不干扰 —— 勾选是「选了哪些行」,单选是「当前是哪一行」, 两套状态各自独立。

示例 ​

微应用尚未挂载。

源码 ​

点击查看源码
vue
<template>
  <div class="demo-wrapper">
    <h3 class="demo-title">Vue 功能列</h3>
    <div class="virt-table-controls">
      <button type="button" class="virt-table-btn virt-table-btn-primary" @click="getChecked">
        获取勾选行
      </button>
      <button type="button" class="virt-table-btn virt-table-btn-danger" @click="clearChecked">
        清空勾选
      </button>
      <button type="button" class="virt-table-btn virt-table-btn-primary" @click="getRadio">
        获取当前行
      </button>
      <button type="button" class="virt-table-btn" @click="clearRadio">清除当前行</button>
    </div>
    <div class="status-text">{{ status }}</div>
    <div style="width: 800px; height: 600px" class="demo-container">
      <VirtTableVue ref="tableRef" :columns="columns" :options="options" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { VirtTableVue, type VueTableColumn } from '@virt-table/vue';
import { faker } from '@faker-js/faker';
import type { VirtTableVueInstance } from '../../virt-table-ref';

/** 序号 / 勾选 / 单选 三种 `col.type` 同表并存(勾选与单选各占一列,状态互不干扰) */
interface Row extends Record<string, unknown> {
  id: number;
  name: string;
  city: string;
  dept: string;
}

const ROW_COUNT = 1000;

const columns: VueTableColumn<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(),
}));

const tableRef = ref<VirtTableVueInstance | null>(null);
const status = ref(`序号 / 勾选 / 单选 三种功能列同表:${ROW_COUNT} 行`);

const options = {
  list,
  itemKey: 'id',
  estimatedSize: 40,
  buffer: 4,
  border: true,
  onCheckChange: (checked: boolean, row: Record<string, unknown>) => {
    const count = (tableRef.value?.getCheckedRows() ?? []).length;
    status.value = `勾选:行 ${(row as Row).id} ${checked ? '选中' : '取消'},共 ${count} 行`;
  },
  onCheckAll: (checked: boolean) => {
    const count = (tableRef.value?.getCheckedRows() ?? []).length;
    status.value = checked ? `全选 ${count} 行` : '取消全选';
  },
  onRadioChange: (row: Record<string, unknown>) => {
    const r = row as Row;
    status.value = `单选:当前行 ${r.name}(id=${r.id})`;
  },
};

const getChecked = () => {
  const rows = (tableRef.value?.getCheckedRows() ?? []) as Row[];
  status.value = rows.length
    ? `勾选了 ${rows.length} 行:${rows.map((r) => r.id).join(', ')}`
    : '没有勾选任何行';
};

const clearChecked = () => {
  tableRef.value?.clearCheckedRows();
  status.value = '已清空勾选';
};

const getRadio = () => {
  const r = tableRef.value?.getSelectedRadio() as Row | null | undefined;
  status.value = r ? `当前行:${r.name}(id=${r.id})` : '没有当前行';
};

const clearRadio = () => {
  tableRef.value?.setSelectedRadio(null);
  status.value = '已清除当前行';
};
</script>

说明 ​

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.value?.setCellRender('42', '__checkbox', { render: () => '🔒' });

穿透只改 UI,不改选择态

勾选框没画出来,但该行仍在选择作用域里 —— 表头「全选」照样会选中它。要真正禁止选中, 在 onCheckChange / onCheckAll 里自己挡。

相关 API ​

字段 / 方法作用详见
col.type功能列类型,六种取值列配置 · 功能列
onCheckChange / onCheckAll勾选变化回调表格配置
getCheckedRows / setCheckedRows / clearCheckedRows勾选态读写实例方法
onRadioChange / getSelectedRadio / setSelectedRadio单选态读写数据与远程 · 单选行
setCellRender单元格级配置,可穿透功能列单元格渲染

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