在React中更新表

时间:2018-10-07 23:54:23

标签: reactjs

看到了一些问题,但仍不确定该怎么做。新的反应。我想使用输入字段使用标题和金额值更新表。我很确定我必须将状态设置为空白表,然后在更新状态时对其进行更新。每次添加2个新值时,如何用新行更新表?

import React, { Component } from 'react';
import './App.css';

const table =
<table id="itemTable">
  <tbody>
  <tr><th>Title</th>
  <th>Amount</th>
  <th>Remove Item</th>
  </tr>
  </tbody>
</table>;

class App extends Component {
constructor(props){
  super(props);
  this.state = {
      title: '',
      amount: '',
      table: {table}
  };

    this.handleChange = this.handleChange.bind(this);
    this.handleClick = this.handleClick.bind(this);
}

    handleClick() {
//adds to table

    }

    handleChange(event) {
        this.setState({
            [event.target.name]: event.target.value
        });
    }

    render() {
    return (
      <section className="App">
          <header>
              <h1>Money Manager</h1>
          </header>

        <section>
          <h1>Finances</h1>
          <form>
              <label htmlFor="name">Name</label>
            <input type="text" name="title" onChange={this.handleChange}/>

              <label htmlFor="amount">Amount</label>
              <input type="text" name="amount" onChange={this.handleChange}/>

            <button type="button" id="add" onClick={this.handleClick}>Add item</button>
          </form>

          <section>
            <h1>Items</h1>
              {table}
          </section>
        </section>
      </section>

    );
  }
}

export default App

1 个答案:

答案 0 :(得分:2)

您应该将表格放在最前面,并为表格的内容创建一个状态,如下所示:

import React, { Component } from 'react';
import './App.css';

class App extends Component {
    constructor(props){
      super(props);
      this.state = {
          tableContent: []
      };

        this.handleChange = this.handleChange.bind(this);
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
      // Don't forget to check if the inputs are corrects

      // Here i generate a random number for the key propriety that react need
      let randomID = Math.floor(Math.random() * 999999);

      // recreate a new object and stock the new line in
      let newTab = this.state.tableContent;
      newTab.push({
        key: randomID,
        title: "",
        amount: "" // Don't forget to get the value of the inputs here
      });

      this.setState({
        tableContent: newTab 
      });

      // Clear the content of the inputs

      // the state has changed, so the tab is updated.
    }

    handleChange(event) {
        this.setState({
            [event.target.name]: event.target.value
        });
    }

    render() {
    return (
      <section className="App">
          <header>
              <h1>Money Manager</h1>
          </header>

        <section>
          <h1>Finances</h1>
          <form>
              <label htmlFor="name">Name</label>
            <input type="text" name="title" onChange={this.handleChange}/>

              <label htmlFor="amount">Amount</label>
              <input type="text" name="amount" onChange={this.handleChange}/>

            <button type="button" id="add" onClick={this.handleClick}>Add item</button>
          </form>

          <section>
            <h1>Items</h1>
            <table id="itemTable">
              <thead>
                <tr>
                  <th>Title</th>
                  <th>Amount</th>
                  <th>Remove Item</th>
                </tr>
              </thead>
              <tbody>
                {this.state.tableContent.map((item) => 
                  <tr key={item.key}>
                    <td>{item.title}</td>
                    <td>{item.amount}</td>
                    <td>
                      {/* Here add the onClick for the action "remove it" on the span */}
                      <span>Remove it</span>
                    </td>
                    <td></td>
                  </tr>
                )}
              </tbody>
            </table>
          </section>
        </section>
      </section>
    );
  }
}

export default App

还没有结束,但是我已经评论了你应该做什么以及我已经做了什么。