-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expand/Collapse TableRow component in Materail UI #4476
Comments
Just remove |
+1 Additionally, the inability to nest TableRows or wrap them in a parent div makes rendering a hidden row (that will later be expanded) very painful. |
Update -- my workaround was to use an expandable card as my first TableRowColumn and adjust the alignment of the rest of the Columns. Porting CardHeader's actAsExpandable property to a TableRow would be very helpful! Hack for anyone else who needs it:
|
In React 16.0+ with material-ui < 1.0 you can easily use Fragments to make collapsable table rows associated with another row:
|
@viotti Do you have a working example on how to use |
@nelsyeung I've just realized that the links on my comment above were broken. I've updated them. Here is my implementation. The key part is
That warning arises from the Collapse element, or any other that renders as a If putting the Collapse inside a cell is not a solution for you, maybe you can put multiple Collapse elements in multiple cells, and make them share state? |
If I'm not mistaken, I believe the above sample will basically expand the 2nd cell of the row, which will make the other cells expanded also. It doesn't feel... native. |
I ended up with something like this after reading the thread: // ...
const styles = (theme) => createStyles({
expandCol: {
width: '5%'
},
expand: {
transform: 'rotate(180deg)',
transition: theme.transitions.create('transform', {
duration: theme.transitions.duration.shortest,
}),
margin: 0,
padding: 0
},
expandOpen: {
transform: 'rotate(0deg)'
},
});
class SomeRow {
// ...
render() {
const expandClasses = classnames(this.props.classes.expand, {
[this.props.classes.expandOpen]: this.state.expanded
});
const details = this.state.expanded ? (
<TableRow>
<TableCell colSpan={3}>
<Collapse in={this.state.expanded} unmountOnExit={true}>hello</Collapse>
</TableCell>
</TableRow>
) : null;
return (
<React.Fragment>
<TableRow key={key}>
<TableCell className={this.props.classes.expandCol}>
<IconButton
className={expandClasses}
onClick={this.handleExpandClick}
aria-expanded={this.state.expanded}
aria-label="Show more">
<ExpandMoreIcon />
</IconButton>
</TableCell>
<TableCell>{this.props.data1}</TableCell>
<TableCell>{this.props.data2}</TableCell>
</TableRow>
{details}
</React.Fragment>
);
}
} |
Hi, for those searching a simple solution to this I just made a simple combination with collapse and table. <TableRow
hover
style={{cursor: 'pointer'}}
onClick={() => this.setState({collapsedRow: !this.state.collapsedRow} )}
>
<TableCell>some content</TableCell>
<TableCell>some content</TableCell>
<TableCell>some content</TableCell>
</TableRow>
<Collapse
in={this.state.collapsedRow}
timeout="auto"
component={collapseComponent}
unmountOnExit
>
{'some content to be collapse'}
</Collapse> and here is the collapseComponent allows the collapse working on all the width of the table : const collapseComponent = (props) => (
<td colSpan={3}> {/* put the number of col of your table in this field */}
<div className={props.className}>
{props.children}
</div>
</td>
) Just put this code inside a normal Table component and it will work perfectly |
@AurelReb Do you know why does the content to be collapsed does not appear gradually when Same as when the collapsed component is closing, it first hides the content THEN the row collapses back. Component does not disappear gradually. |
Hi @ealionel <TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}>
<Collapse
in={this.state.collapsedRow}
timeout='auto'
component={collapseComponent}
unmountOnExit
>
{infos}
</Collapse>
</TableCell>
const collapseComponent = (props) => (
<div className={props.className}>
{props.children}
</div>
) I put the |
@AurelReb Code works fine until the order of rows are changed. If I order by one or another collumn the collapse/expand happens on diffrent row, not on which it should. Any ideas how to fix it? |
@Giedriuuzs Hi, I made an example with a reverse button which reverses the order of the rows. It works well for me. The collapsed Row is still in its expected position. |
@AurelReb Thanks for the example but I have a question. |
@thomastaechoi I just looked at it, and you're right. When I updated my component to fix the transition, I kept my collapseComponent function, but it's useless because Collapse component already uses 'div' as root component. Thank you, I'll update the codesandbox |
i try to make table row collapse with paginnion ` const TableBodyData=(
|
I haven't read every one of the above comments, but the material-ui site has an example of a "Collapsible table": https://material-ui.com/components/tables/#collapsible-table. That example is exactly what I was looking for, and I'm sure many others as well. |
Problem description
I need to expand the TableRow component to open another div containing some fields. But React throws a warning if I try to add divs in the table body.
warning: validateDOMNesting(...): <div> cannot appear as a child of <tr>. See RowComponent > TableRow > tr > div.
Required functionality is similar to
^
button from the nested list component to collapse / expand. Is there any way to customize the material-ui TableRow to expand/collapse ?I'm adding a stripped down version of how I'm trying to add a div in the table body. I have also tried adding the
<TableRow>
within the<div>
and that throws a warning too.Steps to reproduce
Versions
The text was updated successfully, but these errors were encountered: