Using pivots and getTrProps()
Problem Description
I pivot (more than 1 pivot columns) the table as well as dynamically style the selected row using getTrProps. However this causes the groups to collapse each time the row is clicked/styled. Is there a way to prevent this from happening or is there a better way of doing this? I want to indicate which row was selected.
Code Snippet(s)
getTrProps={(state, rowInfo, column) => {
return {
onClick: e => {this.showDetailView(rowInfo)},
style: {
color: (rowInfo != undefined && rowInfo.row != null && rowInfo.row != undefined
&& this.props.currentLineItemDetail !== null && rowInfo.row.cashLineItemId === this.props.currentLineItemDetail.cashLineItemId)
? "#E65100" : null
}
}
}}
Steps to Reproduce
- Specify pivotBy entries on the grid.
- Use getTrProps to style the row that was currently clicked on (something similar to code shown above)
- This causes the groups to collapse.
System Information
- Chrome
Is your showDetailView() causing the data reference to change?
If so you might play around with the collapseOnDataChange: false table setting.
I do remove the styling on the previously highlighted row. I have specified both collapseOnDataChange={false} and freezeWhenExpanded={true}
That's strange then. If you could recreate it on codepen that would be helpful.
http://codepen.io/neojones/pen/XRagjg?editors=1111
If I understand what you're trying to accomplish, then collapseOnDataChange={false} works just fine. Also the removeSelected is unnecessary to remove the styling since the getTrProps is recomputed every time the current row changes.
http://codepen.io/aaronschwartz/pen/dWzzgG?editors=0010
Yes it did work in codepen...the code is however oversimplified. Need to debug my app to see what is happening.
Also you don't even need collapseOnDataChange={false} as long as you use the same references to data, columns, pivotBy, sorting, showFilters, filtering.
http://codepen.io/aaronschwartz/pen/bWrrJL?editors=0010
Yes none of that is changing however I do have conditional logic on the pivotBy field. However I am able to conditionally set the pivotBy field in codepen and it works just fine. :(
In that case you will need the collapseOnDataChange={false} but other than that I'm not sure what is wrong without seeing more of your code. Are you changing filtering or sorting table props?
no sorting and filter props don't change
Ok. Maybe if you paste your entire react-table options I can take a look. You might just have to start simplifying it as much as possible to find the culprit.
Oh ok..so if I add defaultSorting={[{id: 'firstName'}, {id: 'lastName'}]} it collapses. When I remove that...it does not collapse http://codepen.io/neojones/pen/XRagjg
Ah yes. Since the defaultSorting is technically changing since the array reference is changing, it thinks that the sorting has changed.
You can either make the defaultSorting constant: http://codepen.io/aaronschwartz/pen/ZKJaLL?editors=0010
Or you can set collapseOnSortingChange={false}
http://codepen.io/aaronschwartz/pen/QvMOpj?editors=0010
I see. Let me give that a try.
That seems to work. Thanks for your help.