The table table in the element calculates the total and average

(This article only calculates the specific column in the table table total in element_Left hand kisses left face. The blog-CSDN blog is written on the basis of)

I wrote the calculation of a specific aggregate column before, and today I added a calculation of the average value of a specific column~ 

Add attributes and methods to table show-summary :summary-method="getSummaries"

We only need to be in the column that we want to average

Take it out alone

 if (column.property === "secondSupplyWaterTemp") {//哪一列就选哪个
            let sum = 0;
            values.forEach((item) => {
              sum = sum + item;
            });
            sums[index] = sum / values.length;
          }

Here is the getSummaries method:

    getSummaries(param) {
      const { columns, data } = param; //columns是每列的信息,data是每行的信息
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计"; //此处是在index=0的这一列显示为“合计”
          return;
        }
        const values = data.map((item) => Number(item[column.property]));

        // console.log(values);    [2327682, 2322647]
        // console.log(column.property); //hflowAccStart  就是这一列的那个参数名,也是根据这个进行判断 prop绑定的字段

        //截至到这,上面和官网一样的
        //下面开始判断,你想要显示的每一列,去取他们的值返回
        if (
          column.property === "buildArea" ||
          column.property === "secondInstantCool" ||
          column.property === "secondSupplyWaterTemp"
        ) {
          if (column.property === "secondSupplyWaterTemp") {
            let sum = 0;
            values.forEach((item) => {
              sum = sum + item;
            });
            sums[index] = sum / values.length;
          } else {
            //官网是不为空的条件,我改成我想要的列,
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index];
          }
        }
      });

      return sums; //然后....sums就是我想要的结果了
    },

Guess you like

Origin blog.csdn.net/weixin_47194802/article/details/130581784