我正在使用反应表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
谢谢!
答案 0 :(得分: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")
});