1

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?

1 Answer 1

2

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>)
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am familiar with managing state, but how can I access specific rows in the table? For example, if the 5th project is selected, how do I know that it is the 5th one and how can I then modify the 5th entry in projects?
Don't you get all the information in the "e" variable? => e.target.value should contain project.name.
You're right, thank you! Just used the map function to iterate through projects and find the correct one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.