首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >总结反应

总结反应
EN

Stack Overflow用户
提问于 2021-05-28 10:35:57
回答 1查看 45关注 0票数 0

我有一张表,我在那里显示值。

我想要添加一个行,它是每个列的总数。

代码语言:javascript
复制
const MyComponent = () => {
  <Grid item container md={6} direction="row">
    <Table
      className="seasonTable"
      toolbar={false}
      columns={[
        {
          field: "process",
          title: "Proceso",
          render: (row) => {
            return row.process;
          },
        },
        {
          field: "current_season",
          title: "Temporada actual (" + seasons[2].name + ")",
          render: (row) => {
            return row.season1;
          },
        },
        {
          field: "previuos_season",
          title: "Temporada anterior (" + seasons[1].name + ")",
          render: (row) => {
            return row.season2;
          },
        },
        {
          field: "two seasons ago",
          title: "Temporada anterior (" + seasons[0].name + ")",
          render: (row) => {
            return row.season3;
          },
        },
      ]}
      data={filterProcessData()}
    ></Table>
  </Grid>;
};

然后,如图像中所示,显示表。

代码语言:javascript
复制
/**
 * filterProcessData
 */
function filterProcessData() {
  /** @const filterData Array */
  const filterData = [];

  // Filter water foot print
  waterFootprintEvolution.forEach((item) => {
    // If specific process was selected then filter the others
    if (processSelected != "all" && processSelected.id != item.id) {
      return true;
    }

    // Attach data
    // @todo Modify data for the selected seasons
    filterData.push({
      process: item.name,
      season1: item.data[2].total,
      season2: item.data[1].total,
      season3: item.data[0].total,
    });
  });

  return filterData;
}

我完全是新的反应,但我必须做这个改变。

这是我目前所做的工作,我想在每一栏的末尾加上总数。

我怎么能这么做?非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-28 11:35:55

将每一列的和显示为最后一行:

在您的filterData中添加一个附加的总行

代码语言:javascript
复制
function filterProcessData() {
    const filterData = [];

    waterFootprintEvolution.forEach((item) => {
        ...
    });

    const total = { process: "Totals", season1: 0, season2: 0, season3: 0 };
    filterData.forEach(row => {
        total.season1 += row.season1;
        total.season2 += row.season2;
        total.season3 += row.season3;
    });
    filterData.push(total);
    return filterData;
}

将每行的和显示为最后一列:

你就不能为你的total添加一个新列吗?

代码语言:javascript
复制
{
    field: "total",
    title: "Total",
    render: row => {
        return row.total
    }
}

并向被推入filterData的对象中添加一个新的相应的filterData字段。

代码语言:javascript
复制
filterData.push ({
    process: item.name,
    season1: item.data[2].total,
    season2: item.data[1].total,
    season3: item.data[0].total,
    total: item.data[2].total + item.data[1].total + item.data[0].total
});

或者,对列的更改甚至已经足够了(filterData中不需要额外的字段):

代码语言:javascript
复制
{
    field: "total",
    title: "Total",
    render: row => {
        return row.season1 + row.season2 + row.season3
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67737323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档