Vue 分组 + 树形
结合 groupConfig 与树形列,在同一表格中展示分组与层级结构。
示例
微应用尚未挂载。
源码
点击查看源码
vue
<template>
<div class="demo-wrapper">
<h3 class="demo-title">Vue 分组 + 树形</h3>
<div class="virt-table-controls">
<button
type="button"
class="virt-table-btn"
style="background: #10b981; color: #fff"
@click="expandAll"
>
全部展开
</button>
<button
type="button"
class="virt-table-btn"
style="background: #6b7280; color: #fff"
@click="collapseAll"
>
全部收起
</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';
type TableRef = VirtTableVueInstance & {
expandAll: () => void;
collapseAll: () => void;
};
const depts = ['工程部', '设计部', '市场部'];
const teamNames = ['Alpha', 'Beta'];
let uid = 0;
const flatRows: Record<string, unknown>[] = [];
for (const dept of depts) {
for (const team of teamNames) {
const managerCount = 1 + Math.floor(Math.random() * 2);
for (let m = 0; m < managerCount; m++) {
const membersCount = 2 + Math.floor(Math.random() * 4);
flatRows.push({
id: uid++,
name: faker.person.fullName(),
role: '主管',
department: dept,
team: `${dept}-${team}`,
score: Math.floor(Math.random() * 100),
children: Array.from({ length: membersCount }, () => ({
id: uid++,
name: faker.person.fullName(),
role: faker.person.jobTitle(),
department: dept,
team: `${dept}-${team}`,
score: Math.floor(Math.random() * 100),
})),
});
}
}
}
const columns: VueTableColumn[] = [
{ key: 'name', title: '名称', width: 280, type: 'tree' },
{ key: 'role', title: '角色', width: 160 },
{ key: 'department', title: '部门', width: 120 },
{ key: 'team', title: '小组', width: 160 },
{ key: 'score', title: '分数', width: 100 },
];
const options = {
list: flatRows,
itemKey: 'id',
estimatedSize: 40,
buffer: 4,
groupConfig: [{ field: 'department' }, { field: 'team' }],
defaultExpandAll: true,
border: true,
};
const tableRef = ref<TableRef | null>(null);
const status = ref('');
const expandAll = () => {
tableRef.value?.expandAll();
status.value = '全部展开';
};
const collapseAll = () => {
tableRef.value?.collapseAll();
status.value = '全部收起';
};
status.value = '分组 + 树形';
</script>相关 API
| 字段 / 方法 | 作用 | 详见 |
|---|---|---|
groupConfig | 分组配置 | 表格配置 |
col.type: 'tree' | 树形列(分组之下继续按 children 展开) | 列配置 · 功能列 |
defaultExpandAll | 默认全部展开 | 表格配置 |
上面的字段在 Vue 端原样透传(实例方法通过组件
ref调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 VNode、ref转发了哪些方法 —— 见 Vue API。