2 回答

TA貢獻(xiàn)1842條經(jīng)驗 獲得超13個贊
執(zhí)行
我們最初創(chuàng)建了一個表單結(jié)構(gòu)
? 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,都會將表單結(jié)構(gòu)的副本推送到formArray并迭代此表單數(shù)組。
該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>
該組件作為類組件實現(xiàn)。
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貢獻(xiàn)1843條經(jīng)驗 獲得超7個贊
將表格行變成一個單獨的組件。我們稱之為行
// Create a state for the amount of rows you want to view
const [rowCount,setRowCount] = useState(1);
在 render 方法中執(zhí)行以下循環(huán)。
const rowArray = [];
for (let i = 0; i < rowCount; i++) {
rowArray.push(<Row>)
}
將 rowArray 變量插入到 render 方法的返回值內(nèi)。
{rowArray}
您的最終代碼應(yīng)如下所示。
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 關(guān)注
- 154 瀏覽
添加回答
舉報