【发布时间】:2020-05-14 05:45:52
【问题描述】:
我正在尝试为嵌套的对象数组实现一个表格,我想在该列的一个单元格中显示它,就像具有显示更多/显示更少功能的表格一样。
沙盒:https://codesandbox.io/s/react-table-row-table-g8ws3
我也可以将值作为表格数据,但我想通过 Show more / Show less 动态添加这个表格数据
如果项目超过 2,则希望输出类似于 show more,然后是 show less 的切换
我正在为这个应用程序使用react table v6
+-----------+-----------+-----+-----------------+
| firstname | status | age | nested |
+-----------+-----------+-----+-----------------+
| Jack | Submitted | 14 | name value |
| | | | ----------- |
| | | | test1 NA |
| | | | |
| | | | test2 NA |
| | | | |
| | | | Show More/Less |
+-----------+-----------+-----+-----------------+
| Simon | Pending | 15 | name value |
| | | | |
| | | | ----------- |
| | | | |
| | | | test3 NA |
| | | | |
| | | | |
| | | | test4 Go |
| | | | |
| | | | Show More/Less |
+-----------+-----------+-----+-----------------+
import * as React from "react";
import { render } from "react-dom";
import DataGrid from "./DataGrid";
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [],
columns: [],
};
}
componentDidMount() {
this.getData();
this.getColumns();
}
getData = () => {
const data = [
{
firstName: "Jack",
status: "Submitted",
nested: [
{
name: "test1",
value: "NA"
},
{
name: "test2",
value: "NA"
}
],
age: "14"
},
{
firstName: "Simon",
status: "Pending",
nested: [
{
name: "test3",
value: "NA"
},
{
name: "test4",
value: "Go"
}
],
age: "15"
}
];
this.setState({ data });
};
getColumns = () => {
const columns = [
{
Header: "First Name",
accessor: "firstName"
},
{
Header: "Status",
accessor: "status"
},
{
Header: "Age",
accessor: "age"
},
{
Header: "Nested",
id: "nested",
accessor: data =>
data.nested.map(item => (
<div>
<span style={{ marginRight: "10px" }}>{item.name}</span>
<span>{item.value}</span>
</div>
))
}
];
this.setState({ columns });
};
render() {
return (
<>
<DataGrid
data={this.state.data}
columns={this.state.columns}
/>
</>
);
}
}
【问题讨论】:
标签: javascript reactjs ecmascript-6 setstate react-table