I am using React Table in my project, I don't know how to close other expanded rows when user clicks one expanded, i.e. when first, second, and third rows are all expanded, I want to close all of the three when user click the fourth row.
Can someone tell me how?
The way Arnaud Enesvat suggests works for expansion but not to collapse an expanded row back.
I'd suggest his implementation with a change:
handleRowExpanded(rowsState, index) {
this.setState({
expanded: {
[index[0]]: !this.state.expanded[index[0]],
},
});
}
For anyone looking for more info about that :
//in component constructor, add state declaration :
this.state = {
expanded: {}
}
//in component render function - set "expanded" to component state:
expanded={this.state.expanded}
// still in component render() - set an event callback - I prefer to use a dedicated event manager here named handleRowExpanded :
onExpandedChange={(newExpanded, index, event) => this.handleRowExpanded(newExpanded, index, event)}
// then declare an event manager :
handleRowExpanded(newExpanded, index, event) {
this.setState({
// we override newExpanded, keeping only current selected row expanded
expanded: {[index]: true}
});
}
HTH :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With