a-table末尾行添加合计

本文介绍了如何在Vue项目中,使用AntDesign的Table组件,通过手动计算的方式为表格的最后一列添加总计值,通过遍历数据列表并逐项累加特定字段实现汇总功能。

思路:手动计算出合计值,push到最后数组最后列。
在这里插入图片描述

    <a-table
      bordered
      :rowKey="record=>record.id"
      :columns="columns"
      :data-source="sumList"
      :pagination="pagination"
      :scroll="{ y: 'calc(80vh - 530px)', x: true }"
      :customHeaderRow="customHeaderRow"
    >
    </a-table>
data() {
    return {
      pagination: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
        showTotal: val => `总计:${val}`
      },
      dataList: [
        { id: 1, theSsjd: '沙洲街道', theJmzzxq: 2},
	    { id: 2, theSsjd: '南苑街道', theJmzzxq: 2},
      ],
      columns: [
        { title: '序号', customRender: (_, r, i) => (this.pagination.pageNo - 1) * this.pagination.pageSize + i + 1, width: 50, fixed: 'left' },
        { dataIndex: 'theSsjd', title: '所属街道', width: 80, fixed: 'left' },
        { dataIndex: 'theJmzzxq', title: '居民住宅小区(个)', width: 80, },
      ]
    };
  },
  created() {
    // this.getData()
  },
  computed: {
    sumList: function() {
      const sumObject = { id: this.dataList.length + 1, theSsjd: '合计', theJmzzxq: 0 };
      this.dataList.forEach(item => {
        if(typeof item === "object" && item !== null) {
          for (const key in item) {
            if(key !== 'id' && key !== 'theSsjd') {
              if(sumObject[key]) {
                sumObject[key] += item[key]
              } else {
                sumObject[key] = item[key]
              }
            }
          }
        }
      })
      const list = [...this.dataList, sumObject]
      return list
    }
  },
<style lang="less" scoped>

/deep/tr:last-child td {
  background: #fff;
  position: sticky;
  bottom: 0px;
  z-index: 1;
  box-shadow: 5px 0 10px #e4e4e4;
}
</style>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值