繁体   English   中英

如何通过复选框选择表中特定行的全部和取消选择-React Js

[英]How to select all and unselect by specific row in table by checkbox - React Js

我正在尝试通过标题输入复选框选择全部,我可以通过任何特定列取消选择。我这段代码一切正常我正在获取 id 并且我可以获取数据,但我无法通过选择标题输入复选框来选择所有内容,所以请指导我如何为了解决这个问题,我可以全选并取消选择任何特定行,这是我的代码。

import React, { Component } from "react";
import config from "../../Main";
import { withRouter } from "react-router";
import { Helmet } from "react-helmet";
import { withTranslation } from "react-i18next";
import Search from "./Search";
import arrow_left from "../../../img/Aarrow.png";
import "./query"

export class Choose extends Component {
  constructor(props) {
    super(props);
    this.state = {
      checkedBoxCheck: false,
      stats: [],
      selectedItems: [],
    };
    this.toggleHidden = this.toggleHidden.bind(this);
    this.onChange = this.onChange.bind(this);
  }

  async onSubmitt1(e) {
   .....
  }


  async onChange(e) {
    await this.setState({
      [e.target.name]: e.target.value
    });

    this.onSubmitt1("");
  }

  componentDidMount() {
    this.onSubmitt1("");
  }



// This is function of select specific row by id.
  async onItemSelect(row) {
    await this.setState(prevState => ({
      selectedItems: [...prevState.selectedItems, row],
      checkedBoxCheck: true
    }));
    console.log("Hello", this.state.selectedItems);
  }


// This is function to select all rows id
  toggleSelectAll() {
    let selectedItems = [];
    var checkedBoxCheck = !this.state.checkedBoxCheck;
    this.setState({ checkedBoxCheck: checkedBoxCheck });
    this.state.stats.forEach(x => {
      selectedItems[x.id] = x.id

    });
    this.setState(prevState => ({
      selectedItems: [...prevState.selectedItems, selectedItems],
      checkedBoxCheck: !this.state.checkedBoxCheck
    }));

    console.log(selectedItems);
  }

  render() {
    const { stats } = this.state;
    const { t } = this.props;
    if (stats === null) return <p>Loading ....</p>;
    return (
      <div className="container1" style={{ marginTop: "30px" }}>
        <Helmet>
          <title>CHOOSE LIST</title>
        </Helmet>



        <div className="headerr" style={{ marginTop: "20px" }}>
          <div className="invoi caaar">
            <table className="table">
              <thead>
                <tr style={{ marginTop: "-88px" }}>
                  <th class="active" style={{ width: "20px" }}>
                    <input
                      type="checkbox"
                      class="select-all checkbox"
                      onChange={this.onChange}
                      name="first_name"
                      id="selectAll1"
                      onClick={this.toggleSelectAll.bind(this)}
                    />
                  </th>
                  <th className="" style={{ width: "20px" }}>
                    {t("Id")}
                  </th>
                  <th className="">{t("Brand")}</th>
                  <th className="">{t("Model")}</th>
                  <th className="" style={{ width: "90px" }}>
                    {t("production_year")}
                  </th>
                  <th className="t" style={{ width: "90px" }}>
                    {t("technical_inspection")}
                  </th>
                  <th className="t" style={{ width: "90px" }}>
                    {t("insurance_policy")}
                  </th>

                </tr>
              </thead>

              <tbody>
                {stats.map((c, i) => (
                  <tr key={c.id}>
                    <td>
                      <input
                        type="checkbox"
                        // id="togBtn"
                        // checked={this.state.checkedBoxCheck }
                        className="checkbox"
                        name="selectOptions"
                        onClick={() => this.onItemSelect(c.id)}
                      />
                    </td>
                    <td>{i + 1}</td>
                    <td>{c.type ? `${c.type}` : "-"}</td>
                    <td>{c.brand ? `${c.brand}` : "-"}</td>
                    <td>{c.model ? `${c.model}` : "-"}</td>
                    <td>{c.production_year ? `${c.production_year}` : "-"}</td>

                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>
      </div>
    );
  }
}
export default withTranslation()(withRouter(Choose));

谢谢

Codesandbox 寻求帮助: https ://codesandbox.io/s/divine-http-1g883

你唯一的问题应该在这段代码中

toggleSelectAll() {
    // same as above

    // this block
    this.state.stats.forEach(x => {
      // selectedItems[x.id] = x.id
      selectedItems.push(x.id);
    });

    // same as below
  }

您可以更优雅地将此代码替换为:

const selectedItems = this.state.stats.map(x => x.id);

同样在你的渲染函数中改变这个:

    <input
     type="checkbox"
     // id="togBtn"
     checked={this.state.selectedItems.includes(c.id)}
     className="checkbox"
     name="selectOptions"
     onClick={() => this.onItemSelect(c.id)}
     />

暂无
暂无

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

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