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))
相关推荐
梵得儿SHI1 小时前
Vue 3 工程化实践:多页面路由配置 + Pinia 状态管理完全指南
前端·javascript·vue.js·vuerouter4·pinia状态管理的·模块化store设计·路由与状态管理
小李子呢02112 小时前
为什么会有react和vue这些框架的出现
前端·vue.js·react.js
Cobyte2 小时前
5.响应式系统比对:手写 React 响应式状态库 Mobx
前端·javascript·vue.js
码界奇点2 小时前
基于Spring Boot与Vue的教务管理系统设计与实现
vue.js·spring boot·后端·java-ee·毕业设计·源代码管理
1314lay_10072 小时前
axios的Post方法和Delete方法的参数个数和位置不同,导致415错误
前端·javascript·vue.js·elementui
编程小风筝2 小时前
就业信息推荐系统 Python+Django+Vue.js
vue.js·python·django
耗子君QAQ3 小时前
🔧 Rattail | 面向 Vite+ 和 AI Agent 的前端工具链
前端·javascript·vue.js
一只蝉nahc13 小时前
vue使用iframe内嵌unity模型,并且向模型传递信息,接受信息
前端·vue.js·unity
MXN_小南学前端15 小时前
前端开发中 try...catch 到底怎么用?使用场景和最佳实践
javascript·vue.js