【发布时间】:2019-05-20 04:16:10
【问题描述】:
我整天都在 Google 上搜索,试图找到解决问题的方法。 我创建了一个“产品选择页面”,并尝试添加一个“全选”复选框,该复选框将选择显示的任意数量的产品(这将因客户而异)。 它来了,我所有的复选框都在工作,但我不能让“全选”工作。诚然,我正在使用一些内部库,我认为这给我带来了麻烦,因为我无法找到看起来像我到目前为止所做的示例。 好的,创建我的 checkboxGroup 的代码在这里:
let productSelectionList = (
<FormGroup className="productInfo">
<Field
component={CheckboxGroup}
name="checkboxField"
vertical={true}
choices={this.createProductList()}
onChange={this.handleCheckboxClick}
helpText="Select all that apply."
label="Which accounts should use this new mailing address?"
/>
</FormGroup>
);
如您所见,我的选择将在 createProductList 方法中创建。看起来像这样:
createProductList() {
const { products } = this.props;
const selectAllCheckbox = <b>Select All Accounts</b>;
let productList = [];
productList.push({ label: selectAllCheckbox, value: "selectAll" });
if (products && products.length > 0) {
products.forEach((product, idx) => {
productList.push({
label: product.productDescription,
value: product.displayArrangementId
});
});
}
return productList;
}
另外请注意,这里我还创建了“选择所有帐户”条目,然后将其推送到列表中,值为“selectAll”。然后推送实际产品,每个产品都有一个标签和一个值(尽管只显示标签。最终结果如下所示: Select Products checkboxes 我已经设法用这个功能隔离了“全选”复选框:
handleCheckboxClick(event) {
// var items = this.state.items.slice();
if (event.selectAll) {
this.setState({
'isSelectAllClicked': true
});
} else {
this.setState({
'isSelectAllClicked': false
});
}
}
我也创建了这个componentDidUpdate函数:
componentDidUpdate(prevProps, prevState) {
if (this.state.isSelectAllClicked !== prevState.isSelectAllClicked && this.state.isSelectAllClicked){
console.log("if this ", this.state.isSelectAllClicked);
console.log("if this ", this.props);
} else if (this.state.isSelectAllClicked !== prevState.isSelectAllClicked && !this.state.isSelectAllClicked){
console.log("else this ", this.state.isSelectAllClicked);
console.log("else this ", this.props);
}
}
所以在控制台中,我可以看到,当单击“全选”复选框时,我得到一个“真”标志,而取消单击它我得到一个“假”。但是现在我该如何选择剩余的框(我承认我对 React/Redux 非常陌生,而且我以前没有任何复选框经验)。 在 Chrome 中,我可以看到我的 this.props,如下所示。 this.props
您可以看到 this.props.productList.values.checkboxField 显示“全选”复选框以及四个产品的值为 true。但那是因为我为这个拥有 14 种产品的测试成员手动检查了这四种产品。我怎样才能“全选”来选择所有 14 种产品? 我是不是走错了路? (请告诉我这仍然可行):(
【问题讨论】:
-
您的问题是在全选时没有检查剩余的复选框,还是问题实际上是获取了这些复选框所代表的所有数据?或者两者兼而有之?
-
第一个。我一直无法找出正确的方法来编码如何检查所有其他框。如何让每个产品都将“this.props.productList.values.checkboxField”设置为“true”?
标签: reactjs checkbox react-redux