1

我不熟悉 React,希望能得到一些帮助。我在表格中有一个表格,其中每一行都是一个具有状态(“待批准”或“已批准”)的项目。我在每一行也有一个复选框。I would like it so that when the checkbox next to a row is selected and the form is submitted, I can change the status of each selected project to 'Approved.'

<Form>
    <FormGroup>
        <Button type="submit" onClick {this.submitClicked}>Approve Projects</Button>
    </FormGroup>
<Table bordered={true}>
    <thead>
        <tr>
            <th>Select</th>
            <th>Project Name</th>
            <th>Project Number</th>
            <th>Project Status</th>
            <th>Min Size</th>
            <th>Max Size</th>
        </tr>
    </thead>
    <tbody>
        {projects.map((project: Project) =>
            <tr key={project.projectNumber}>
                <td>               
                    <FormControl
                        type="checkbox"
                        id="selected"
                        value={project.projectName}
                        onChange={e => this.handleChange(e)}
                    />
                </td>
                <td>{project.projectName}</td>
                <td>{project.projectNumber}</td>
                <td>{project.status}</td>
                <td>{project.minSize}</td>
                <td>{project.maxSize}</td>
            </tr>
        )}
    </tbody>
</Table>
</Form>

我的 handleChange 函数应该包含什么,我应该如何去做?我想知道是否有某种方法可以将选定的项目添加到数组中,然后更改它们的状态值?

4

1 回答 1

2

您可以使用带有state的组件。这允许您管理组件内部的状态。因此,您可以在示例中对其进行操作并更改项目的状态。更多关于这里:状态类

例子:

class YourComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {projects: props.projects};
  }

   handleChange = (e) => {
      let projects = this.state.projects;
      // manipulate your project here
      this.setState({
         projects: projects
      });
   }

   render(){
      return (<Form>
         (...)
      </Form>)
   }
}
于 2018-04-05T20:10:36.850 回答