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))
相关推荐
香香爱编程16 小时前
vue3自定义顶部弹窗
前端·javascript·vue.js
蜡台16 小时前
Vue Echart 的 **高阶组件化** 封装思路
前端·javascript·vue.js·echarts
xuankuxiaoyao16 小时前
vue.js 路由第二篇
前端·javascript·vue.js
一 乐16 小时前
图书电子商务网站系统|基于SprinBoot+vue图书电子商务网站设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·图书电子商务网站系统
吃乔巴的糖1 天前
Vue 3 打印模板设计器 (print-canvas-designer)
前端·vue.js
如果超人不会飞1 天前
后端别再手绘了!TinyVue 流程图组件 Flowchart 跨端定制指南
vue.js
cc.ChenLy1 天前
大文件断点续传原理总结和Demo示例详解
javascript·vue.js·文件上传·大文件断点续传
程序员祥云1 天前
VUE2_TO_VITE_VUE3
javascript·vue.js·ecmascript
苏瞳儿1 天前
vue3+pinia+mqtt实时响应连接
前端·javascript·vue.js
i220818 Faiz Ul1 天前
理财系统|基于java+vue的家庭理财系统小程序(源码+数据库+文档)
java·vue.js·spring boot·小程序·论文·毕设·理财系统