element表格多行合计

效果

 

<el-table
            :data="tableData"
            border
            row-key="id"
            default-expand-all
            show-summary
            :summary-method="getSummaries"
            :tree-props="{ children: 'children' }"
         >
            <el-table-column label="序号">
               <template slot-scope="props">
                  <span v-if="props.row.order">{{ props.row.order }}</span>
                  <span v-else>合计</span>
               </template>
            </el-table-column>
            <el-table-column label="船型" prop="ship"> </el-table-column>
            <el-table-column label="船名" prop="shipName"> </el-table-column>
            <el-table-column label="船员数量" prop="peopleNum">
               <template slot-scope="props">
                  {{
                     props.row.peopleNum
                        | calculaColumn(props.row, "peopleNum", tableData)
                  }}
               </template>
            </el-table-column>
            <el-table-column label="施工区域" prop="area"> </el-table-column>
            </el-table-column>
            <el-table-column label="挖泥时长(h)" prop="dredgrTime">
               <template slot-scope="props">
                  {{
                     props.row.dredgrTime
                        | calculaColumn(props.row, "dredgrTime", tableData)
                  }}
               </template>
            </el-table-column>
            <el-table-column label="抛泥时长(h)" prop="throwrTime">
               <template slot-scope="props">
                  {{
                     props.row.throwrTime
                        | calculaColumn(props.row, "throwrTime", tableData)
                  }}
               </template>
            </el-table-column>
            <el-table-column label="备注" prop="remarks"> </el-table-column>
         </el-table>

使用过滤器

filters: {
      /**
       * 表格添加末尾合计栏
       * @param val 当前值
       * @param row 当前行
       * @param field 当前字段
       * @param data 表格数据
       */
      calculaColumn(val, row, field, data) {
         // 如果有id即数据项,正常输出本值
         if (row.id) {
            return row[field];
         }
         // 没有id则是最后合计项
         let sums = 0;
         data.map((item) => {
            sums += Number(item[field]);
         });
         return sums;
      },
   },

element自带的合计方法

/**
       * 表格生成合计栏
       */
      getSummaries(param) {
         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 (!values.every((value) => isNaN(value) || value === 0)) {
               sums[index] = values.reduce((prev, curr) => {
                  const value = Number(curr);
                  if (!isNaN(value) && value > 0) {
                     return prev + curr;
                  } else {
                     return prev;
                  }
               }, 0);
            } else {
               sums[index] = "";
            }
         });

         return sums;
      },

构造一个空数据行

mounted() {
      // 初始化数据
      this.init();
      // 地图
      this.$nextTick(() => {
         this.importMap(0);
         this.tableData.push({
            id: "",
            ship: "",
            shipName: "",
            peopleNum: "",
            area: "",
            dredgPeriod: "",
            dredgrTime: "",
            throwrTime: "",
            overflowTime: "",
            load: "",
            party: "",
            position: "",
            remarks: "",
         });
      });
      // 构造表格序号
      this.tableData.map((item, index) => {
         item.order = index + 1;
         item.children.map((child, childindex) => {
            child.order = index + 1 + "." + (childindex + 1);
         });
         return item;
      });
   },

### 实现 Element Plus 表格多行合计功能 为了在 `Element Plus` 的表格组件中实现多行合计功能,可以通过自定义计算属性来动态汇总每一列的数据,并将其展示在表格的最后一。具体方法如下: #### 方法概述 通过监听数据变化并实时更新总计数值,在模板部分利用 `<slot>` 插槽机制向表格追加一为总计。 #### 代码示例 ```html <template> <el-table :data="tableData" style="width: 100%"> <!-- 定义常规列 --> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="amount1" label="金额1"> <template #default="scope">{{ scope.row.amount1 }}</template> </el-table-column> <el-table-column prop="amount2" label="金额2"> <template #default="scope">{{ scope.row.amount2 }}</template> </el-table-column> <!-- 总计 --> <el-table-column fixed="bottom" align="right"> <template #footer> <span>总计</span> <span>{{ totalAmount1 }} / {{ totalAmount2 }}</span> </template> </el-table-column> </el-table> </template> <script lang="ts"> import { defineComponent, computed } from 'vue'; export default defineComponent({ setup() { const tableData = [ { name: "张三", amount1: 100, amount2: 200 }, { name: "李四", amount1: 300, amount2: 400 } ]; // 计算总和 const totalAmount1 = computed(() => tableData.reduce((acc, cur) => acc + Number(cur.amount1), 0)); const totalAmount2 = computed(() => tableData.reduce((acc, cur) => acc + Number(cur.amount2), 0)); return { tableData, totalAmount1, totalAmount2 }; } }); </script> ``` 此段代码展示了如何创建一个简单的带有总计表格[^1]。这里使用了 Vue Composition API 来管理状态以及计算属性来进求和运算。注意,实际应用时可能还需要考虑更多细节,比如处理不同类型的字段、格式化数字等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值