gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:26:54 25 4
gpt4 key购买 nike

我正在尝试通过标题输入复选框选择所有内容,并且可以通过任何特定列取消选择。此代码一切正常,我正在获取 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)}
/>

关于javascript - 如何通过复选框选择表中特定行的全部和取消选择 - React Js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60719954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com