简体   繁体   English

如何在ReactJS中使用数组数组呈现表?

[英]How to render tables using an array of arrays in ReactJS?

I have a page that has an array of arrays with different groups and members. 我有一个页面,其中包含具有不同组和成员的数组的数组。 What I'm trying to achieve is to create a table that displays the project name and the members in it as sown below: 我要实现的目标是创建一个表,该表显示项目名称及其中的成员,如下所示:

------------ ------------ 
| Project1 | | Project2 | 
------------ ------------  
| student1 | | student2 |
------------ ------------  
| student2 | | student3 | 
------------ ------------  

------------ ------------ 
| Project3 | | Project4 | 
------------ ------------  
| student4 | | student6 |
------------ ------------  
| student5 | | student7 | 
------------ ------------ 

On the actual dataset, some groups might have more students, and there are about 50 groups. 在实际的数据集上,某些小组可能有更多的学生,并且大约有50个小组。 Below, is my failed attempt at rendering a table similar to the one above 下面是我未能渲染类似于上图的表的尝试

import React, { Component } from "react";
import { Row, Container, Col, Form, FormGroup, Label, Input, Button, FormText, FormFeedback } from "reactstrap";

class ViewGroups extends Component {
  constructor(props) {
    super(props);

    this.state = {
      groups: [
        {
          projectName: "Project 1",
          students: [
            {
              name: "student1",
              GPA: "3.4"
            },
            {
              name: "student2",
              GPA: "3.2"
            }
          ] 
        },
        {
          projectName: "Project 2",
          students: [
            {
              name: "student3",
              GPA: "3.4"
            },
            {
              name: "student4",
              GPA: "3.9"
            }
          ] 
        },
        {
          projectName: "Project 3",
          students: [
            {
              name: "student5",
              GPA: "3.0"
            },
            {
              name: "student6",
              GPA: "3.1"
            }
          ] 
        },
        {
          projectName: "Project 4",
          students: [
            {
              name: "student7",
              GPA: "3.0"
            },
            {
              name: "student8",
              GPA: "3.5"
            }
          ] 
        }
    ]
    }
  }


  renderTableData() {
    return this.state.groups.map((group, index) => {
       const { projectName } = group 
       return (
          <tr>
             <td>{projectName}</td>

          </tr>
       )
    })
 }

  render() {
    return (
      <Container>
        <Form>
          <Col>
            <h1>Groups</h1>
          </Col>
          <div>
          <table id='groups'>
               <tbody>
                  {this.renderTableData()} 
               </tbody>
            </table>
          </div>
        </Form>
      </Container>
    );
  }
}

export default ViewGroups;

You will need to render the student data for each group in some way. 您将需要以某种方式呈现每个组的学生数据。 Something like this: 像这样:

renderTableData() {
  return this.state.groups.map((group, index) => {
     const { projectName, students } = group 
     return (
        <tbody>
           <tr>
             <td>{projectName}</td>
           </tr>
           {students.map((student) => (
             <tr key={student.name}>
               <td>{student.name}</td>
             </tr>
           })
        </tbody>
     )
  })
}

and remove the wrapper tbody. 并拆下包装体。

You'll have to move the columns around to match what you want, but this is the general idea to render the inner student data. 您必须四处移动列以匹配您想要的内容,但这是呈现内部学生数据的一般想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM