【问题标题】:Implementing Show More/ Show Less for table present in a cell: ReactJS为单元格中的表格实现显示更多/显示更少:ReactJS
【发布时间】: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


    【解决方案1】:

    首先您需要在将数据设置为状态后决定将呈现多少数据,默认状态将是带有“显示更多”按钮的两个元素,因此,您需要触发一个状态for 是否单击了按钮,并且根据此状态呈现的项目应该是, 要将按钮插入表格,您需要将其作为最后一个子元素插入到数组中,而无需仅填充列表末尾的按钮

    对于嵌套列内的更多嵌套显示,我们需要类似的逻辑,但对于较小的组件,为此,我们需要将其分开以使其具有自己的状态

    这是我的尝试:

    import * as React from "react";
    import { render } from "react-dom";
    import DataGrid from "./DataGrid";
    import Nested from "./Nested.js";
    
    class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          items: [],
          data: [],
          columns: [],
          clicked: false,
          text: "show more"
        };
      }
    
      componentDidMount() {
        this.getData();
        this.getColumns();
      }
    
      onClick = () => {
        this.setState(
          prevState => {
            const text = !prevState.clicked ? "show less" : "show more";
            return {
              clicked: !prevState.clicked,
              text
            };
          },
          () => this.formatData()
        );
      };
    
      getData = () => {
        const data = [
          {
            firstName: "Jack",
            status: "Submitted",
            nested: [
              {
                name: "test1",
                value: "NA"
              },
              {
                name: "test2",
                value: "NA"
              },
              {
                name: "test5",
                value: "NA"
              }
            ],
            age: "14"
          },
          {
            firstName: "Simon",
            status: "Pending",
            nested: [
              {
                name: "test3",
                value: "NA"
              },
              {
                name: "test4",
                value: "Go"
              },
              {
                name: "test6",
                value: "NA"
              },
              {
                name: "test7",
                value: "NA"
              }
            ],
            age: "15"
          },
          {
            firstName: "Simon2",
            status: "Pending",
            nested: [
              {
                name: "test3",
                value: "NA"
              },
              {
                name: "test4",
                value: "Go"
              }
            ],
            age: "15"
          }
        ];
        this.setState(() => ({ data }), () => this.formatData());
      };
    
      getColumns = () => {
        const columns = [
          {
            Header: "First Name",
            accessor: "firstName"
          },
          {
            Header: "Status",
            accessor: "status"
          },
          {
            Header: "Age",
            accessor: "age"
          },
          {
            Header: "Nested",
            id: "nested",
            accessor: data => <Nested data={data.nested} />
          }
        ];
        this.setState({ columns });
      };
    
      formatData = () => {
        const clickBtn = {
          firstName: <button onClick={this.onClick}>{this.state.text}</button>
        };
        if (this.state.clicked) {
          const items = [...this.state.data, clickBtn];
          this.setState({ items });
        } else {
          const items = [...this.state.data.slice(0, 2), clickBtn];
          this.setState({ items });
        }
      };
    
      render() {
        return (
          <>
            <DataGrid data={this.state.items} columns={this.state.columns} />
          </>
        );
      }
    }
    
    render(<App />, document.getElementById("root"));
    
    
    

    和嵌套文件:

    import * as React from "react";
    
    class Nested extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          items: [],
          data: [],
          clicked: false,
          text: "show more"
        };
      }
    
      componentDidMount() {
        this.setState(() => ({ data: this.props.data }), () => this.formatData());
      }
    
      onClick = () => {
        this.setState(
          prevState => {
            const text = !prevState.clicked ? "show less" : "show more";
            return {
              clicked: !prevState.clicked,
              text
            };
          },
          () => this.formatData()
        );
      };
    
      formatData = () => {
        console.log(this.state.data);
    
        const clickBtn = {
          type: "btn",
          component: <button onClick={this.onClick}>{this.state.text}</button>
        };
        if (this.state.clicked) {
          const items = [...this.state.data, clickBtn];
          this.setState({ items });
        } else {
          const items = this.state.data && [
            ...this.state.data.slice(0, 2),
            clickBtn
          ];
          this.setState({ items });
        }
      };
    
      render() {
        const { items } = this.state;
        return (
          <>
            {items &&
              items.map(item =>
                item.type === "btn" ? (
                  !!item.component && item.component
                ) : (
                  <div>
                    <span style={{ marginRight: "10px" }}>{item.name}</span>
                    <span>{item.value}</span>
                  </div>
                )
              )}
          </>
        );
      }
    }
    
    export default Nested;
    
    

    沙盒:https://codesandbox.io/s/react-table-row-table-zpiyo

    【讨论】:

    • 感谢您的回复。我希望在这个嵌套表所在的单元格本身内显示更多/显示更少的东西(问题中的第四列)。检查有问题的表结构:) 我不想将额外的行添加到主表中,只有放置此嵌套表的单元格我必须添加此显示更多/显示更少功能
    • 不好意思误会了
    • 我添加了修改
    • 非常感谢:) 是的,唯一缺少的部分是向此嵌套表添加标题。现在它只显示对象的值,因为行和标题没有显示。见问题中的表格,第四列有标题nameage
    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多