User9123
User9123

Reputation: 99

Making a table with a checkbox that updates table values in React

I'm unfamiliar with React and would appreciate some help. I have a table within a form where each row is a project that has a status (either 'Pending Approval' or 'Approved'). I also have a checkbox in each row. 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>

What should go in my handleChange function and how should I go about doing this? I was wondering if there was some way to add the selected projects to an array and then change their status value?

Upvotes: 1

Views: 3961

Answers (1)

bombek
bombek

Reputation: 584

You can use components with state. That allows you to manage a state inside a component. Thus you can manipulate it and change the projects' statuses in your example. More about this here: state classes

Example:

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>)
   }
}

Upvotes: 2

Related Questions