el-table 合计 可保留小数

 

 

 

   <el-table
          ref="pageTable"
          :data="tableData"
          border
          style="width: 100%"
          :summary-method="getSummaries"
          show-summary
        >

  不考虑保留小数

  getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = "合计";
        } else {
          if (index === 3) {
            const values = data.map((item) => Number(item[column.property]));
            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] = "N/A";
            }
          }
        }
      });
      return sums;
    },

  如果需要保留小数

getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 1) {
          sums[index] = "合计";
        } else {
          if (index === 3) {
            const values = data.map((item) =>
              Number(item[column.property].answer)
            );
            console.log(values, "values");
            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] = "N/A";
              sums[index] = "";
            }
            if (sums[index]) {
              sums[index] = sums[index].toFixed(2); //保留2位小数
            }
          }
        }
      });

      return sums;
    },

  

posted @ 2024-11-20 17:47  ThisCall  阅读(20)  评论(0编辑  收藏  举报