【element-ui笔记】element中el-table(合计)计算指定列

1.需求:我只需要前面三列的合计,最后一列不需要

【element-ui笔记】element中el-table(合计)计算指定列_第1张图片
找了很久,看到element-ui里面的show-summary方法,以及自定义的:summary-method,觉得很合适
【element-ui笔记】element中el-table(合计)计算指定列_第2张图片
以下是官网说明:
【element-ui笔记】element中el-table(合计)计算指定列_第3张图片

实现代码如下:

 <el-table
          :data="tableData"
          class="table"
          fit
          size="medium"
          show-summary
          :summary-method="getSummaries"
        >
          <el-table-column
      prop="id"
      label="ID"
      width="180">
    </el-table-column>
    <el-table-column
      prop="name"
      label="姓名">
    </el-table-column>
    <el-table-column
      prop="amount1"
      sortable
      label="rewardAmount">
    </el-table-column>
    <el-table-column
      prop="amount2"
      sortable
      label="winningNum">
    </el-table-column>
    <el-table-column
      prop="amount3"
      sortable
      label="cardNum">
    </el-table-column>
</el-table>
 //指定列求和
	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 (column.property === 'winningNum' ||column.property ==='rewardAmount' || column.property==='cardNum' ) {
							sums[index] = values.reduce((prev, curr) => {
							 const value = Number(curr);
							 if (!isNaN(value)) {
							   return prev + curr;
							 } else {
							   return prev;
							 }
							}, 0);
							sums[index];
 
				     }
				});
				return sums
	    	},

你可能感兴趣的:(Vue,element-ui)