【问题标题】:React-table add total as first rowReact-table 将总计添加为第一行
【发布时间】:2019-03-16 21:21:05
【问题描述】:

我正在使用 react-table 6.9.2。我希望能够在顶部(标题正下方)添加一行,显示每列的总数。

这是我的函数,它只输出带有静态值的表:

function App() {
  const data = [
    {
      one: 2,
      two: 10.4,
      three: 10.1,
      four: 54,
      five: 5,
      six: 5,
      seven: 10,
      eight: 10,
      nine: 10
    },
    {
      one: 1,
      two: 10.4,
      three: 10.1,
      four: 54,
      five: 5,
      six: 5,
      seven: 10,
      eight: 10,
      nine: 10
    }
  ];
  return (
    <div className="App">
      <ReactTable
        data={data}
        showPagination={false}
        columns={[
          {
            Header: "Sales Overview",
            columns: [
              {
                Header: "one",
                id: "one",
                accessor: "one",
                show: true
              },
              {
                Header: "two",
                accessor: "two",
                show: true
              },
              {
                Header: "three",
                id: "three",
                accessor: "three",
                show: true
              },
              {
                Header: "four",
                id: "four",
                accessor: "four",
                show: true
              },
              {
                Header: "five",
                id: "five",
                accessor: "five",
                show: true
              },
              {
                Header: "six",
                id: "six",
                accessor: "six",
                show: true
              },
              {
                Header: "seven",
                id: "seven",
                show: true,
                accessor: "seven"
              },
              {
                Header: "eight",
                id: "eight",
                show: true,
                accessor: "eight"
              },
              {
                Header: "nine",
                id: "nine",
                accessor: "nine",
                show: true
              }
            ]
          }
        ]}
        loading={false}
        minRows={1}
        className="-striped -highlight"
      />
    </div>
  );
}

有没有办法可以在顶部添加总行?即使我得到数据中发送的每列的总值,我可以命令它始终在顶部显示总行吗?

如果有帮助,我还创建了一个沙箱来显示表格:https://codesandbox.io/s/2vz3z741op

提前致谢!

【问题讨论】:

  • 一个简单的解决方法是丢弃主标题(销售概览),并使用子标题来保存聚合列(总行数)
  • @jgoday 你能给我举个例子吗?我不明白它是如何工作的
  • 好的,codesandbox.io/s/xp19oy14np。 React-table 似乎只支持直到 2 个嵌套标题,因此您可以使用第二级来模拟总页脚。对不起,代码缩进和样式混乱,但我在一台旧笔记本电脑上,键盘有问题......
  • @jgoday 看起来不错,谢谢!

标签: reactjs react-table


【解决方案1】:

您可以在数据数组中添加一个总计条目:

https://codesandbox.io/s/qv7vjpr69

const getTotals = (data, key) => {
  let total = 0;
  data.forEach(item => {
    total += item[key];
  });
  return total;
};
function App() {
  const data = [
    {
      one: "first row",
      two: 10.4,
      ...
    },
    {
      one: "second row",
      two: 10.4,
      ...
    }
  ];
  data.unshift({
    one: "totals",
    two: getTotals(data, "two"),
    three: getTotals(data, "three"),
    four: getTotals(data, "four"),
    five: getTotals(data, "five"),
    six: getTotals(data, "six"),
    seven: getTotals(data, "seven"),
    eight: getTotals(data, "eight"),
    nine: getTotals(data, "nine")
  });

【讨论】:

    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 2020-01-07
    • 2020-10-19
    • 2020-03-20
    • 1970-01-01
    • 2018-10-26
    相关资源
    最近更新 更多