使用复选框制作 table 以更新 React 中的 table 值
Making a table with a checkbox that updates table values in React
我不熟悉 React,希望得到一些帮助。我在表单中有一个 table,其中每一行都是一个具有状态('Pending Approval' 或 'Approved')的项目。我在每一行中也有一个复选框。 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 函数应该包含什么,我应该如何去做?我想知道是否有某种方法可以将选定的项目添加到数组中,然后更改它们的状态值?
您可以使用具有 状态 的组件。这允许您管理组件内的状态。因此,您可以操纵它并更改示例中项目的状态。更多信息请点击此处:state classes
示例:
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>)
}
}
我不熟悉 React,希望得到一些帮助。我在表单中有一个 table,其中每一行都是一个具有状态('Pending Approval' 或 'Approved')的项目。我在每一行中也有一个复选框。 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 函数应该包含什么,我应该如何去做?我想知道是否有某种方法可以将选定的项目添加到数组中,然后更改它们的状态值?
您可以使用具有 状态 的组件。这允许您管理组件内的状态。因此,您可以操纵它并更改示例中项目的状态。更多信息请点击此处:state classes
示例:
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>)
}
}