Ant Design Vue中 table组件设置分组表头和固定总结栏

功能如图所示:

官方文档:

https://www.antdv.com/components/table-cn#components-table-demo-grouping-columns

https://www.antdv.com/components/table-cn#components-table-demo-summary

分组表头的配置主要是这个,就是套娃原理,不需要展示数据的直接写个title就行,需要展示数据的字段才需要详细的配置属性。
javascript 复制代码
{
    title: 'Other',
    children: [
      {
        title: 'Age',
        dataIndex: 'age',
        key: 'age',
        width: 200,
        sorter: (a: TableDataType, b: TableDataType) => a.age - b.age,
      },
      {
        title: 'Address',
        children: [
          {
            title: 'Street',
            dataIndex: 'street',
            key: 'street',
            width: 200,
          },
          {
            title: 'Block',
            children: [
              {
                title: 'Building',
                dataIndex: 'building',
                key: 'building',
                width: 100,
              },
              {
                title: 'Door No.',
                dataIndex: 'number',
                key: 'number',
                width: 100,
              },
            ],
          },
        ],
      },
    ],
 },
配置固定的合计栏,主要是使用了table中的总结栏插槽summary。
javascript 复制代码
<template #summary>
      <a-table-summary fixed>
        <a-table-summary-row>
          <a-table-summary-cell :index="0" :col-span="2" style="text-align: center"
            >子合计</a-table-summary-cell
          >
          <a-table-summary-cell
            v-for="(col, i) in summaryLeafColumns.slice(2)"
            :key="String(col.dataIndex ?? i)"
            :index="2 + i"
          >
            {{ tableDataInfo?.total[col.dataIndex] }}
          </a-table-summary-cell>
        </a-table-summary-row>
        <a-table-summary-row v-if="showAllTotal">
          <a-table-summary-cell :index="0" :col-span="2" style="text-align: center"
            >总计</a-table-summary-cell
          >
          <a-table-summary-cell
            v-for="(col, i) in summaryLeafColumns.slice(2)"
            :key="`g-${String(col.dataIndex ?? i)}`"
            :index="2 + i"
          >
            {{ totalData[col.dataIndex] }}
          </a-table-summary-cell>
        </a-table-summary-row>
      </a-table-summary>
</template>

扁平化多级表头函数:

javascript 复制代码
/** 扁平化多级表头,得到叶子列(顺序与表格列一致) */
export function flattenLeafColumns(columns: ColumnNode[] | undefined): ColumnNode[] {
  if (!columns?.length) return []
  const out: ColumnNode[] = []
  for (const col of columns) {
    if (col.children?.length) {
      out.push(...flattenLeafColumns(col.children))
    } else {
      out.push(col)
    }
  }
  return out
}

代码:

javascript 复制代码
const props = defineProps({
  tableDataInfo: {
    type: Object,
    dault: () => ({}),
  },
  // 是否显示总计行
  showAllTotal: {
    type: Boolean,
    default: false,
  },
})


const summaryLeafColumns = computed(() => flattenLeafColumns(currentColumns))
相关推荐
mayaairi11 小时前
Vue2 组件通讯(二):ref、自定义事件与provide/inject实战
前端·javascript·vue.js
李少兄13 小时前
记一次 IDEA 打开 Vue 项目后目录“闪现即消失“ 问题排查
java·vue.js·intellij-idea
leoZ23114 小时前
2026-09-08-mysql-57-init-walkthrough
前端·javascript·数据库·vue.js·opencv·mysql·adb
qq_3508972114 小时前
trea实用工具
vue.js·uniapp
paopaokaka_luck14 小时前
非遗文物数字化系统(AI非遗问答、ONNX图像识别、协同过滤推荐、ECharts数据分析、非遗知识浏览与互动、文创商城订单闭环、文化活动报名签到、社区交流)
前端·javascript·vue.js·人工智能·数据分析·echarts
BS30813_vx15 小时前
31754+基于 Spring Boot + Vue 的网络文学交流分享平台设计与实现
vue.js·spring boot·后端
mayaairi16 小时前
Vue2 组件通讯(一):Props传值完全指南
前端·javascript·vue.js
码艺-Alimjan16 小时前
Tauri 2.x + Vue 3 桌面应用开发实战:从踩坑到完美落地
前端·javascript·vue.js·rust·typescript·go
宿6741 天前
vue3-包管理器
前端·vue.js
mayaairi1 天前
Vue2 生命周期完全指南:从创建到销毁的8个钩子函数
前端·javascript·vue.js