Vue 自动合计行
开启 showSummary,为列配置 summary: 'sum'|'avg'|'count'|'max'|'min' 或 summaryMethod 自定义聚合。合计基于筛选/排序后的数据动态计算,占用表尾。
示例
微应用尚未挂载。
源码
点击查看源码
vue
<template>
<div class="demo-wrapper">
<h3 class="demo-title">Vue 自动合计行</h3>
<div class="demo-hint">
底部合计行自动聚合:数量求和、单价求平均、小计自定义汇总。合计随筛选/排序动态更新。
</div>
<div style="width: 720px; height: 480px" class="demo-container">
<VirtTableVue :columns="columns" :options="options" />
</div>
</div>
</template>
<script setup lang="ts">
import { VirtTableVue, type VueTableColumn } from '@virt-table/vue';
import { faker } from '@faker-js/faker';
interface Row extends Record<string, unknown> {
id: number;
name: string;
qty: number;
price: number;
total: number;
}
const columns: VueTableColumn<Row>[] = [
{ key: 'id', title: 'ID', width: 80 },
{ key: 'name', title: '商品', width: 200 },
{ key: 'qty', title: '数量', width: 140, align: 'right', summary: 'sum' },
{ key: 'price', title: '单价', width: 140, align: 'right', summary: 'avg' },
{
key: 'total',
title: '小计',
width: 160,
align: 'right',
summaryMethod: (values) =>
'¥' + (values as number[]).reduce((a, b) => a + (b || 0), 0).toFixed(2),
},
];
const list: Row[] = Array.from({ length: 800 }, (_, i) => {
const qty = faker.number.int({ min: 1, max: 20 });
const price = faker.number.int({ min: 5, max: 500 });
return { id: i + 1, name: faker.commerce.productName(), qty, price, total: qty * price };
});
const options = {
list,
itemKey: 'id',
estimatedSize: 40,
buffer: 6,
border: true,
showSummary: true,
summaryText: '合计',
};
</script>相关 API
| 字段 / 方法 | 作用 | 详见 |
|---|---|---|
showSummary / summaryText | 开启合计行与首列标签 | 数据与远程 · 合计行 |
col.summary | 'sum' | 'avg' | 'count' | 'max' | 'min' | 数据与远程 · 合计行 |
col.summaryMethod | `(values, rows) => string | number` |
合计基于筛选 / 排序后的叶子行,随视图自动重算。分组表头下表尾也按列树渲染,分组格的聚合语义见多级分组表尾;客户端分页时算的是当前页。
上面的字段在 Vue 端原样透传(实例方法通过组件
ref调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 VNode、ref转发了哪些方法 —— 见 Vue API。