Javascript 反应:如果在onRowSelection中使用setState,则无法选择物料ui tableRow
我有一个材料界面 我想把选中的行传递给DELETE按钮Javascript 反应:如果在onRowSelection中使用setState,则无法选择物料ui tableRow,javascript,reactjs,material-design,state,Javascript,Reactjs,Material Design,State,我有一个材料界面 我想把选中的行传递给DELETE按钮 constructor(props) { super(props); this.state = { selectedRows: 'none', }; } onRowSelection(val){ console.log(this); this.setState({ selectedRows: val, }); } re
constructor(props) {
super(props);
this.state = {
selectedRows: 'none',
};
}
onRowSelection(val){
console.log(this);
this.setState({
selectedRows: val,
});
}
render() {
<Table
fixedHeader={true}
selectable={true}
multiSelectable={true}
onRowSelection={this.onRowSelection.bind(this)}
>
...
<TableFooter adjustForCheckbox={true}>
<TableRow>
<TableRowColumn colSpan="5" style={{textAlign: 'right'}}>
<RaisedButton
primary={true}
label="DELETE"
labelPosition="after"
icon={<ActionDelete/>}
onClick={this.props.onDelete.bind(this.state.selectedRows)}
/>
</TableRowColumn>
</TableRow>
</TableFooter>
</Table>
}
构造函数(道具){
超级(道具);
此.state={
selectedRows:“无”,
};
}
在线选择(val){
console.log(this);
这是我的国家({
selectedRows:val,
});
}
render(){
...
我认为这里的问题是您使用bind
函数的方式。bind
函数的第一个参数应该是,第二个参数是您希望传递给结果函数的参数。因此,您应该像这样将函数传递给onClick
onClick={this.props.onDelete.bind(this, this.state.selectedRows)}
材质ui的表在每次渲染时重置其选中状态时存在问题。
要解决此问题,您需要使用setTimeout恢复选中状态,并自己管理选中的阵列。至少在修复此错误之前。
在该州,应具备以下条件:
state = {
selectedRows: [],
};
渲染时,添加以下内容:
<TableRow key={index}
selected={this.state.selectedRows.indexOf(index) !== -1}>
</TableRow>
及
您还应该添加一个按钮,允许用户取消选中所有选项。我认为选择消失的原因是。setState()会重新加载,与此效果相同。forceUpdate()。不幸的是,它没有解决问题。
onRowSelection(selectedRows) {
if (selectedRows.length === 0) { // due to a bug in material-ui
setTimeout(() => { this.setState({ selectedRows: this.state.selectedRows }) }, 100);
return;
};
this.setState({ selectedRows });
}
<Table
onRowSelection={this.onRowSelection}
constructor(props) {
super(props);
this.onRowSelection = this.onRowSelection.bind(this);
}