javascript - 如何在 React.js 中动态添加和删除表格行

标签 javascript reactjs

最佳答案

您可以通过使用 react 状态来实现这一点。在您的情况下,您使用的是嵌套对象,因此更新它们时需要小心。

改变状态并不是一个好主意,因为它很容易导致错误或意外行为。

仔细观察处理函数是如何工作的。

Here您有现场演示。

class App extends React.Component {
  state = {
    rows: []
  };
  handleChange = idx => e => {
    const { name, value } = e.target;
    const rows = [...this.state.rows];
    rows[idx] = {
      [name]: value
    };
    this.setState({
      rows
    });
  };
  handleAddRow = () => {
    const item = {
      name: "",
      mobile: ""
    };
    this.setState({
      rows: [...this.state.rows, item]
    });
  };
  handleRemoveRow = () => {
    this.setState({
      rows: this.state.rows.slice(0, -1)
    });
  };
  render() {
    return (
      <div>
        <div className="container">
          <div className="row clearfix">
            <div className="col-md-12 column">
              <table
                className="table table-bordered table-hover"
                id="tab_logic"
              >
                <thead>
                  <tr>
                    <th className="text-center"> # </th>
                    <th className="text-center"> Name </th>
                    <th className="text-center"> Mobile </th>
                  </tr>
                </thead>
                <tbody>
                  {this.state.rows.map((item, idx) => (
                    <tr id="addr0" key={idx}>
                      <td>{idx}</td>
                      <td>
                        <input
                          type="text"
                          name="name"
                          value={this.state.rows[idx].name}
                          onChange={this.handleChange(idx)}
                          className="form-control"
                        />
                      </td>
                      <td>
                        <input
                          type="text"
                          name="mobile"
                          value={this.state.rows[idx].mobile}
                          onChange={this.handleChange(idx)}
                          className="form-control"
                        />
                      </td>
                    </tr>
                  ))}
                </tbody>
              </table>
              <button
                onClick={this.handleAddRow}
                className="btn btn-default pull-left"
              >
                Add Row
              </button>
              <button
                onClick={this.handleRemoveRow}
                className="pull-right btn btn-default"
              >
                Delete Row
              </button>
            </div>
          </div>
        </div>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

PD: If I can give you a recommendation I'd say that you need to study a little bit more about react - javascript to move forward because it would helpful to achieve things like this faster, right now you need to understand the basics pretty good.

关于javascript - 如何在 React.js 中动态添加和删除表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49171107/

相关文章:

javascript - Canvas:模拟具有缩放和旋转功能的相机

javascript - 从 React Hook 复制一个数组

javascript - 类型错误 : Cannot read property 'token' of undefined error in reactjs login page

javascript - 如何为静态托管网站设置 Cloud Firestore

javascript - Three.js 中的动态骨骼动画

javascript - 使用html为iphone浏览器创建自动完成框

javascript - React 组件的 <svg> 元素字符串

javascript - 如何在 ReactJS 中从 “outside” 访问组件方法?

reactjs - 找不到雪包 "abstracts/variables"。你安装了吗?运行 npm start 时出现此错误

javascript - jQuery/Javascript - 如何在按钮的值更改时触发事件?