亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在單擊時將新的輸入行添加到表中?

如何在單擊時將新的輸入行添加到表中?

Go
滄海一幻覺 2023-08-21 16:03:10
下面是我的 React 項目的一個組件。該組件的基礎是一個最初顯示一個輸入行的表格。import React, { Component } from "react";import { Navbar, Table, Form } from "react-bootstrap";import Nav from "./Nav";import "../client/scss/import.scss";// import Logo from "./images/logo.svg";class Import extends Component {  state = {    collapseID: ""  };  render() {    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>                <tr>                  <td>                    <Form.Control as="select">                      <option>Veteran Affairs</option>                      <option>Older Australians</option>                      <option>Disability Sickness Carers</option>                      <option>Report Employment Income</option>                    </Form.Control>                  </td>                  <td>                    <input                      className="form-control"                      type="date"                      value="2011-08-19"                      id="example-date-input"                    />                  </td>                  <td>                    <input                      className="form-control"                      type="date"                      value="2011-08-19"                      id="example-date-input"                    />                  </td>    );  }}export default Import;在底部您將看到該<button id="addRow">+ Add</button>元素。每次單擊此按鈕時,我希望將一個新行附加到現有表中,以便其他輸入字段可見。我應該使用 jQuery 來實現這個嗎?有人可以協助指導我如何做到這一點嗎?下面是當前在我的瀏覽器上顯示的屏幕截圖(如果有幫助的話):
查看完整描述

2 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

如果您正在尋找成熟的解決方案,可以使用以下解決方案。

執行

我們最初創建了一個表單結構

? 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>

? ? );

? }

}



查看完整回答
反對 回復 2023-08-21
?
藍山帝景

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;



查看完整回答
反對 回復 2023-08-21
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號