在React JS中选中复选框时无法获取值

时间:2019-04-30 09:31:48

标签: javascript reactjs

我有一个表,其中标题具有带有SelectAll属性的复选框,并且表中的每一行都具有一个复选框,单个复选框和全选所有复选框均有效,现在我正在尝试获取选中时的Id值,如果我取消选择它,它将被添加到数组中。

以下是我到目前为止尝试过的内容:

import React, { Component } from "react";
import ReactDOM from "react-dom";
import Table from "react-bootstrap/Table";

import "./styles.css";

const exampleData = [
  { Id: 1, Name: "name 1", Source: "source 1", Number: "number 1" },
  { Id: 2, Name: "name 2", Source: "source 2", Number: "number 2" },
  { Id: 3, Name: "name 3", Source: "source 3", Number: "number 3" },
  { Id: 4, Name: "name 4", Source: "source 4", Number: "number 4" },
  { Id: 5, Name: "name 5", Source: "source 5", Number: "number 5" }
];

class App extends Component {
  constructor() {
    super();
    this.state = {
      selectAll: false,
      items: exampleData.map(item => ({ ...item, checked: false }))
    };
  }
  selectAll = () => {
    this.setState({ selectAll: !this.state.selectAll }, () => {
var collection = [];
    for (const item of this.state.items) {
        collection.push(item.Id);
      }

      let items = [...this.state.items];
      items = items.map(item => {
        return {
          ...item,
          checked: this.state.selectAll
        };
      });
      this.setState({ items });
    });
  };
  handleTransClick = (e, id) => {
    e.preventDefault();
    alert("You clicked row #" + id);
  };
  handleCheckbox = id => {
var collection2 = [];
collection2.push(id);
    this.setState({
      items: this.state.items.map(item => {
        return {
          ...item,
          checked: item.Id === id ? !item.checked : item.checked
        };
      })
    });
  };
  render() {
    const result = this.state.items;
    return (
      <Table striped bordered className="App">
        <thead>
          <tr>
            <th className="col-md-2">
              <input
                onChange={this.selectAll}
                type="checkbox"
                checked={this.state.selectAll}
              />
            </th>
            <th>Id</th>
            <th>Name</th>
            <th>Source</th>
            <th>Number</th>
          </tr>
        </thead>
        <tbody>
          {result.map((item, i) => (
            <tr key={i}>
              <td className="col1">
                <input
                  type="checkbox"
                  name={item.Name}
                  checked={item.checked ? true : ""}
                  onChange={this.handleCheckbox.bind(this, item.Id)}
                />
              </td>
              <td className="col2">
                <a href="" onClick={e => this.handleTransClick(e, item.Id)}>
                  {item.Id}
                </a>
              </td>
              <td className="col3">{item.Name}</td>
              <td className="col4"> {item.Source}</td>
              <td className="col5"> {item.Number}</td>
            </tr>
          ))}
        </tbody>
      </Table>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

如何根据复选框的选中值控制它们?

2 个答案:

答案 0 :(得分:0)

      else{

  this.setState({
    items : this.state.items.filter(function(val) {return val!==value})
  })

}

答案 1 :(得分:0)

public class User { private String name; private int age; ... } 方法的开头,您有一些不相关的代码,但这不会阻止它的运行。我看到的唯一潜在问题是,由于handleCheckbox的异步特性,您应该对setState使用函数参数:

this.setState