在element的<el-table>中对固定列添加合计

本文介绍如何在Element UI的表格组件中添加固定列并实现列的合计功能。通过`summary-method`属性结合`getSummaries`方法,处理了合计值可能被遮挡的问题,并展示了如何根据数据计算指定列的总计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在element的中对固定列添加合计

在中添加

         :summary-method="getSummaries"
          show-summary

添加getSummaries方法

 getSummaries(param) {
      //处理合计被整个table表遮挡问题
      this.$nextTick(() => {
        this.$refs.table.doLayout();
      });
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if(column.property === '对应table中需要合计的字段名称'){
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
          } else {
            sums[index] = ' ';
          }
        }else {
          sums[index] = ' ';
        }
      });
      return sums
    }
<template> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection" width="55"/> <el-table-column label="产品名称" width="220"> <template #default="scope"> <div class="product-name-wrapper"> <el-link type="primary" :underline="false" @click="goToProductDetail(scope.row.id)"> <span >{{ scope.row.name }}</span> <span style="color: brown;" >※点击进入详细画面</span> </el-link> </div> </template> </el-table-column> <el-table-column property="date" label="产品分类"width="120"/> <el-table-column property="date" label="产品单价"width="120"/> <el-table-column property="date" label="产品名称"width="120"/> <el-table-column property="date" label="产品分类"width="120"/> <el-table-column property="date" label="产品单价"width="120"/> <el-table-column property="date" label="生产日期"width="120"/> <el-table-column property="date" label="单价"width="120"/> <el-table-column property="date" label="库存"width="120"/> <el-table-column property="date" label="购买数量"width="120"/> <el-table-column property="date" label="合计"width="120"/> <el-table-column label="操作" width="180"> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.$index,scope.row)"> 编辑 </el-button> <el-button size="small" type="danger" @click="handleDelete(scope.$index,scope.row)"> 删除 </el-button> </template> </el-table-column> </el-table> </template> <script setup> const router = useRouter(); const goToProductDetail = (productId) => { router.push(`/home/test${productId}`) } const handleEdit = (index, row) => { console.log(index, row) } const handleDelete = (index, row) => { console.log(index, row) } const tableData = [ { date: '2016-05-04', name: 'Aleyna', address: 'Lohrbergstr. 86c, Süd Lilli, Saarland', }, { date: '2016-05-03', name: 'Helen', address: '760 A Street, South Frankfield, Illinois', }, { date: '2016-05-02', name: 'Brandon', address: 'Arnold-Ohletz-Str. 41a, Alt Malinascheid, Thüringen', }, { date: '2016-05-01', name: 'Margie', address: '23618 Windsor Drive, West Ricardoview, Idaho', } ] </script> <style scoped> .aaa-tag { display: inline-block; background-color: #409eff; color: white; font-size: 12px; padding: 0 4px; border-radius: 3px; margin-right: 5px; } </style>   我想在购买数量中上数字输入框左边号右边减号
最新发布
07-10
<template> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection" width="55"/> <el-table-column label="产品名称" width="220"> <template #default="scope" > <el-link type="primary" :underline="false" @click="goToProductDetail(scope.row.id)"> <span class="aaa-tag"></span>{{ scope.row.name }} <span style="color: brown;">※点击进入详细画面</span> </el-link> </template> </el-table-column> <el-table-column property="date" label="产品分类"width="120"/> <el-table-column property="date" label="产品单价"width="120"/> <el-table-column property="date" label="产品名称"width="120"/> <el-table-column property="date" label="产品分类"width="120"/> <el-table-column property="date" label="产品单价"width="120"/> <el-table-column property="date" label="生产日期"width="120"/> <el-table-column property="date" label="单价"width="120"/> <el-table-column property="date" label="库存"width="120"/> <el-table-column property="date" label="购买数量"width="120"/> <el-table-column property="date" label="合计"width="120"/> <template #default="scope"> <el-button size="small" @click="handleEdit(scope.$index, scope.row)"> Edit </el-button> <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)"> Delete </el-button> </template> </el-table> </template> <script setup> const router = useRouter(); const goToProductDetail = (productId) => { router.push(`/home/test${productId}`) } const handleEdit = (index, row) => { console.log(index, row) } const handleDelete = (index, row) => { console.log(index, row) } const tableData = [ { date: '2016-05-04', name: 'Aleyna', address: 'Lohrbergstr. 86c, Süd Lilli, Saarland', }, { date: '2016-05-03', name: 'Helen', address: '760 A Street, South Frankfield, Illinois', }, { date: '2016-05-02', name: 'Brandon Deckert', address: 'Arnold-Ohletz-Str. 41a, Alt Malinascheid, Thüringen', }, { date: '2016-05-01', name: 'Margie Smith', address: '23618 Windsor Drive, West Ricardoview, Idaho', } ] </script> <style scoped> .aaa-tag { display: inline-block; background-color: #409eff; color: white; font-size: 12px; padding: 0 4px; border-radius: 3px; margin-right: 5px; } </style> 报错
07-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值