第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

Go
滄海一幻覺 2023-08-21 16:03:10
下面是我的 React 項目的一個組件。該組件的基礎(chǔ)是一個最初顯示一個輸入行的表格。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>元素。每次單擊此按鈕時,我希望將一個新行附加到現(xiàn)有表中,以便其他輸入字段可見。我應(yīng)該使用 jQuery 來實現(xiàn)這個嗎?有人可以協(xié)助指導(dǎo)我如何做到這一點嗎?下面是當(dāng)前在我的瀏覽器上顯示的屏幕截圖(如果有幫助的話):
查看完整描述

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>

? ? );

? }

}



查看完整回答
反對 回復(fù) 2023-08-21
?
藍(lán)山帝景

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;



查看完整回答
反對 回復(fù) 2023-08-21
  • 2 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號