簡體   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