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))
相关推荐
花归去2 小时前
vue3中 function getText(){} 、 const getText=()=>{} ;区别在哪里,优缺点
javascript·vue.js·ecmascript
web行路人3 小时前
前端对Commands(斜杠命令)一些常用
前端·javascript·vue.js·vue
饺子不吃醋3 小时前
深入理解 Vue 3 的 setup(含 Composition API)
前端·vue.js
UXbot5 小时前
AI画原型工具如何帮非设计师快速生成UI界面
前端·vue.js·ui·kotlin·swift·原型模式·web app
invicinble6 小时前
前端框架使用vue-cli( 第二层:工程配置层--4.axios需要做的基础配置)
前端·vue.js·前端框架
MXN_小南学前端6 小时前
Vue + Element UI 分页器封装:比直接用 el-pagination 更省心的通用方案
javascript·vue.js·elementui
亲亲小宝宝鸭6 小时前
Vue3中那些冷门但实用的方法
前端·vue.js
M ? A6 小时前
Vue 转 React | VuReact 实时监听开发指南
前端·vue.js·后端·react.js·面试·开源·vureact
半兽先生6 小时前
vue高性能下拉组件 支持上万数据不卡顿
前端·javascript·vue.js