[英]How to append values to an array from checkbox?
I have checkboxes on a table that, when checked, I would like to append values to an array from the data populating the table (element.link in my code below). 我在表格上具有复选框,选中该复选框时,我想将填充表格数据的值追加到数组中(下面代码中的element.link)。
I already have an toggleRow
method that is called when a checkbox is clicked, but how can I append the matching element.link
to an array when a checkbox is clicked? 我已经有一个单击复选框时会调用的toggleRow
方法,但是单击复选框时如何将匹配的element.link
追加到数组中?
componentDidMount() {
const _self = this;
fetch(config.api.urlFor('xxx'))
.then((response) => response.json())
.then((data) => {
const tableContent = [];
data.array.forEach(element => {
tableContent.push({
date: element.date,
title: element.title,
link: <Link to='' onClick={(e)=> {e.preventDefault(); this.getPreauthorizedLink(element.link)}}>Report</Link>
});
})
this.setState({reportJSON: tableContent || [], tableIsBusy: false})
})
.catch((err) => _self.setState({tableIsBusy: false }));
}
toggleRow(name, id) {
const newSelected = Object.assign({}, this.state.selected);
if(newSelected[name]){
delete newSelected[name]
}else {
newSelected[name] = !this.state.selected[name];
}
this.setState({
selected: newSelected,
selectAll: 2
});
}
toggleSelectAll() {
let newSelected = {};
if (this.state.selectAll === 0) {
this.state.data.forEach(x => {
newSelected[x.firstName] = true;
});
}
}
render() {
const {reportJSON} = this.state;
const content = (
<div className="container">
<div className="card mb-3">
<div className="card-header">
<div className="float-left">
<i className="fas fa-list mr-1"></i>
xxx
</div>
<div className="float-right">
<DownloadReportsButton />
</div>
</div>
<div className="card-body">
<div className="table-responsive">
<Busy isBusy={this.state.tableIsBusy}>
<ReactTable
data={reportJSON}
filterable
defaultFilterMethod={(filter, row) =>
String(row[filter.accessor]) === filter.value}
columns={[
{
id: "checkbox",
accessor: "",
Cell: ({ original }) => {
return (
<input
type="checkbox"
className="checkbox"
checked={this.state.selected[original.firstName] === true}
onChange={() => this.toggleRow(original.firstName)}
/>
);
},
Header: x => {
return (
<input
type="checkbox"
className="checkbox"
checked={this.state.selectAll === 1}
ref={input => {
if (input) {
input.indeterminate = this.state.selectAll === 2;
}
}}
onChange={() => this.toggleSelectAll()}
/>
);
},
sortable: false,
width: 45
},
{ Header: 'Date',
accessor: 'date',
maxWidth: 120,
filterMethod: (filter, rows) =>
matchSorter(rows, filter.value, { keys: ["date"] }),
filterAll: true},
{ Header: 'Title',
accessor: 'title',
filterMethod: (filter, rows) =>
matchSorter(rows, filter.value, { keys: ["title"] }),
filterAll: true},
{ Header: 'Link',
accessor: 'link',
maxWidth: 120},
]}
loading={this.state.tableIsBusy}
/>
</Busy>
</div>
</div>
<div className="card-footer small text-muted">Updated yesterday at 11:59 PM</div>
</div>
</div>
)
Which array are you trying to append the element.link
to? 您尝试将element.link
附加到哪个数组?
Simply using the array.push()
method is the easiest way to append a value to an array. 简单地使用array.push()
方法是将值附加到数组的最简单方法。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/push
const arr = []
toggleRow(element) {
arr.push(element.link)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.