2 回答
TA贡献1842条经验 获得超12个赞
执行
我们最初创建了一个表单结构
const initialForm = {
id:1,
queueType:'',
startDate:'2011-08-19',
endDate:'2011-08-19',
week:'',
startTime:'13:45:00',
endTime:'13:45:00',
capacity:0
}
每次按下时Add,都会将表单结构的副本推送到formArray并迭代此表单数组。
该setFormValue()功能将更新formArray.
setFormValue = (e, index)=>{
let {formArray} = this.state;
formArray[index][e.target.name] = e.target.value;
this.setState({
formArray
})
}
更新表单在dom中渲染(为了看到实时更新),我们使用
<pre>
{JSON.stringify(this.state.formArray)}
</pre>
该组件作为类组件实现。
import React, { Component } from "react";
import { Navbar, Table, Form } from "react-bootstrap";
const initialForm = {
id:1,
queueType:'',
startDate:'2011-08-19',
endDate:'2011-08-19',
week:'',
startTime:'13:45:00',
endTime:'13:45:00',
capacity:0
}
export default class Hello extends Component {
constructor(props){
super(props);
this.state = {
collapseID: "",
formArray:[
{
...initialForm
}
]
};
this.addNewForm = this.addNewForm.bind(this);
}
addNewForm = ()=>{
let newForm = {...initialForm};
newForm.id = this.state.formArray[this.state.formArray.length -1].id +1;
this.setState(prevState =>{
return {
...prevState,
formArray:[...prevState.formArray, newForm]
}
})
}
setFormValue = (e, index)=>{
let {formArray} = this.state;
formArray[index][e.target.name] = e.target.value;
this.setState({
formArray
})
}
removeForm=(id)=>{
const formArray = this.state.formArray.filter(form => form.id != id)
this.setState({formArray})
}
render() {
return (
<div>
<div className="import">
<h1>Import New Schedule</h1>
<div className="importTable">
<Table className="table table-hover" id="tables">
<thead className="thead-dark">
<tr>
<th>Queue</th>
<th>Start Date</th>
<th>End Date</th>
<th>Day of Week</th>
<th>Time Start</th>
<th>Time End</th>
<th>Capacity</th>
</tr>
</thead>
<tbody>
{this.state.formArray.map((form,index)=>(
<tr key={form.id}>
<td>
<select name="queueType"
defaultValue={form.queueType}
onChange={()=>this.setFormValue(event,index)}>
<option>Veteran Affairs</option>
<option>Older Australians</option>
<option>Disability Sickness Carers</option>
<option>Report Employment Income</option>
</select>
</td>
<td>
<input
className="form-control"
type="date"
name="startDate"
defaultValue={form.startDate}
onChange={()=>this.setFormValue(event,index)}
id="example-date-input"
/>
</td>
<td>
<input
className="form-control"
type="date"
name="endDate"
defaultValue={form.endDate}
onChange={()=>this.setFormValue(event,index)}
id="example-date-input"
/>
</td>
<td>
<select name="week" defaultValue={form.week}
onChange={()=>this.setFormValue(event,index)}>
<option>Monday</option>
<option>Tuesday</option>
<option>Wednesday</option>
<option>Thursday</option>
<option>Friday</option>
<option>Saturday</option>
<option>Sunday</option>
</select>
</td>
<td>
<input
className="form-control"
type="time"
name="startTime"
defaultValue={form.startTime}
onChange={()=>this.setFormValue(event,index)}
id="example-time-input"
/>
</td>
<td>
<input
className="form-control"
type="time"
name="endTime"
defaultValue={form.endTime}
onChange={()=>this.setFormValue(event,index)}
id="example-time-input"
/>
</td>
<td>
<input
className="form-control"
type="number"
defaultValue={form.capacity}
onChange={()=>this.setFormValue(event,index)}
name="capacity"
id="example-number-input"
/>
</td>
<td>
<button type="button" onClick={()=>this.removeForm(form.id)}>- Remove</button>
</td>
</tr>
))}
</tbody>
</Table>
<button id="addRow" onClick={this.addNewForm}>+ Add</button>
</div>
</div>
<pre>
{JSON.stringify(this.state.formArray)}
</pre>
</div>
);
}
}
TA贡献1843条经验 获得超7个赞
将表格行变成一个单独的组件。我们称之为行
// Create a state for the amount of rows you want to view
const [rowCount,setRowCount] = useState(1);
在 render 方法中执行以下循环。
const rowArray = [];
for (let i = 0; i < rowCount; i++) {
rowArray.push(<Row>)
}
将 rowArray 变量插入到 render 方法的返回值内。
{rowArray}
您的最终代码应如下所示。
class Import extends Component {
state = {
collapseID: "",
rowCount: 1,
};
_addRow = () => {
const { rowCount } = this.state;
this.setState(rowCount + 1);
}
render() {
const { rowCount } = this.state;
const rowArray = [];
for (let i = 0; i < rowCount; i++) {
rowArray.push(<Row>)
}
return (
<div>
<Nav />
<div className="import">
<h1>Import New Schedule</h1>
<div className="importTable">
<Table className="table table-hover" id="tables">
<thead className="thead-dark">
<tr>
<th>Queue</th>
<th>Start Date</th>
<th>End Date</th>
<th>Day of Week</th>
<th>Time Start</th>
<th>Time End</th>
<th>Capacity</th>
</tr>
</thead>
<tbody>
{rowArray}
</tbody>
</Table>
<button id="addRow" onClick={_addRow}>+ Add</button>
</div>
</div>
</div>
);
}
}
export default Import;
- 2 回答
- 0 关注
- 109 浏览
添加回答
举报