How to collapse other expanded rows in react-table
Asked Answered
R

6

7

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?

Reproof answered 6/11, 2017 at 15:21 Comment(0)
B
18

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 :)

Blodgett answered 11/11, 2017 at 14:49 Comment(2)
The key here is to let the parent component (the component actually rendering the react-table) orchestrate the state. +1 for pointing out that expanded={this.state.expanded}.Phthalein
how to do it in functional component al i see exapanded is read-only, any solutions will help. thank youScatterbrain
N
9

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]],
    },
  });
}
Notecase answered 9/4, 2018 at 14:43 Comment(0)
L
3

I've found help from Nathan Zylbersztejn at spectrum.chat/thread/ea9b94dc-6291-4a61-99f7-69af4094e90c :

<ReactTable
    ...
    expanded={this.state.expanded}
    onExpandedChange={(newExpanded, index, event) => {
        if (newExpanded[index[0]] === false) {
            newExpanded = {}
        } else {
            Object.keys(newExpanded).map(k => {
                newExpanded[k] = parseInt(k) === index[0] ? {} : false
            })
        }
        this.setState({
            ...this.state,
            expanded: newExpanded
        })
    }}
/>

I hope it helps somebody.

Lorinalorinda answered 4/10, 2018 at 17:56 Comment(0)
P
0
// this line will go into constructor (keep every accordion closed initially)
this.state = {
  expanded: {}
}

// this line will go into react table (will keep only one accordian open at any time)
expanded={this.state.expanded}
onExpandedChange={expandedList => {
    const expanded = {}
    Object.keys(expandedList).forEach(item => {
        const expand = Boolean(expandedList[item] && expandedList[item].constructor === Object)
        expanded[item] = expand
    })
    this.setState({ expanded })
}}
Plausible answered 8/1, 2020 at 11:17 Comment(0)
R
-2

Ok, I figured it out by myself in the end. By dynamically change the "expanded" props of the react-table, I was able to control show only one expanded row each time.

Reproof answered 7/11, 2017 at 2:29 Comment(3)
How do you fix for expand a single row at a time? Please share the code with your fixing.Legofmutton
It's good to answer your own questions, but I think you should post some code here to show whatyou've done.Perpetuate
Here is code sample <ReactTable columns={columns} data={newsData} minRows={10} loading={loading} className="-striped -highlight" collapseOnDataChange={false} expanded={this.state.expanded} SubComponent={row => { return <NewsOptions newsItem={row.original} />; }} onExpandedChange={(newExpanded, index, event) => this.handleRowExpanded(newExpanded, index, event)} />Legofmutton
J
-3
onExpandedChange = (expanded, index, event) => {
    this.setState({ expanded: { [index]: expanded[index] !== false } })
}
Jada answered 29/7, 2019 at 10:15 Comment(2)
Could you please add some explanation about how and why your code snippet provides an answer to the question? Thank you.Heal
@Heal i google this issue, i found this code, i used it in my project, it worked, i share, if you are react developer, you're looking for solution for this issue, you will know how and why, that's itJada

© 2022 - 2024 — McMap. All rights reserved.