繁体   English   中英

单击react.js在数组中添加和删除项目

[英]add and remove item in array on click reactjs

我正在使用reactjs和ant设计在一个简单的表上工作。

我的计划是在单击按钮时在列表上添加和删除新项目。

我的问题是我不知道该怎么做。

我尝试遵循此主题,但没有运气。

希望你能理解我。

谢谢。

样例代码

function remove() {
  console.log("remove");
}
function add() {
  console.log("add");
}

const columns = [
  {
    title: "Num",
    dataIndex: "num"
  },
  {
    title: "Name",
    dataIndex: "name"
  },
  {
    title: "Age",
    dataIndex: "age"
  },
  {
    title: "Address",
    dataIndex: "address"
  }
];
const data = [
  {
    key: "1",
    num: 1,
    name: "John Brown",
    age: 32,
    address: "New York No. 1 Lake Park"
  },
  {
    key: "2",
    num: 2,
    name: "Jim Green",
    age: 42,
    address: "London No. 1 Lake Park"
  },
  {
    key: "3",
    num: 3,
    name: "Joe Black",
    age: 32,
    address: "Sidney No. 1 Lake Park"
  }
];

<Table pagination={false} columns={columns} dataSource={data} />
    <Button type="primary" onClick={add}>
      add
    </Button>
    <Button type="danger" onClick={remove}>
      remove
    </Button>

您需要使用react state 状态保存数据,当您要addremove ,更新此状态并做出反应以重新呈现表。

我已经更新了您的代码。 单击addadd一个新的随机行。 单击“删除”后,将删除最后一行。

CodeSandbox

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table, Button } from "antd";

function remove() {
  console.log("remove");
}
const columns = [
  {
    title: "Num",
    dataIndex: "num"
  },
  {
    title: "Name",
    dataIndex: "name"
  },
  {
    title: "Age",
    dataIndex: "age"
  },
  {
    title: "Address",
    dataIndex: "address"
  }
];
let data = [
  {
    key: "1",
    num: 1,
    name: "John Brown",
    age: 32,
    address: "New York No. 1 Lake Park"
  },
  {
    key: "2",
    num: 2,
    name: "Jim Green",
    age: 42,
    address: "London No. 1 Lake Park"
  },
  {
    key: "3",
    num: 3,
    name: "Joe Black",
    age: 32,
    address: "Sidney No. 1 Lake Park"
  }
];

export default class MyTable extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: data
    };
  }
  add = () => {
    var row = {
      key: "99",
      num: 99,
      name: "I am New",
      age: 32,
      address: "New Address"
    };
    var newStateArray = [...this.state.data];
    newStateArray.push(row);
    this.setState(() => {
      return {
        data: newStateArray
      };
    });
  }

remove = () => {
  var newStateArray = [...this.state.data];
  if(newStateArray.length > 1) {
    newStateArray.pop();
  }
  this.setState(() => {
    return {
      data: newStateArray
    };
  });
}
  render() {
    return (
      <div>
        <Table
          pagination={false}
          columns={columns}
          dataSource={this.state.data}
        />
        <Button type="primary" onClick={this.add}>
          add
        </Button>
        <Button type="danger" onClick={this.remove}>
          remove
        </Button>
      </div>
    );
  }
}
ReactDOM.render(<MyTable />, document.getElementById("container"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM