table icon indicating copy to clipboard operation
table copied to clipboard

Using pivots and getTrProps()

Open rachel-singh opened this issue 9 years ago • 15 comments

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

  1. Specify pivotBy entries on the grid.
  2. Use getTrProps to style the row that was currently clicked on (something similar to code shown above)
  3. This causes the groups to collapse.

System Information

  • Chrome

rachel-singh avatar May 03 '17 17:05 rachel-singh

Is your showDetailView() causing the data reference to change? If so you might play around with the collapseOnDataChange: false table setting.

aaronschwartz avatar May 03 '17 17:05 aaronschwartz

I do remove the styling on the previously highlighted row. I have specified both collapseOnDataChange={false} and freezeWhenExpanded={true}

rachel-singh avatar May 03 '17 18:05 rachel-singh

That's strange then. If you could recreate it on codepen that would be helpful.

aaronschwartz avatar May 03 '17 18:05 aaronschwartz

http://codepen.io/neojones/pen/XRagjg?editors=1111

rachel-singh avatar May 03 '17 19:05 rachel-singh

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

aaronschwartz avatar May 03 '17 20:05 aaronschwartz

Yes it did work in codepen...the code is however oversimplified. Need to debug my app to see what is happening.

rachel-singh avatar May 03 '17 20:05 rachel-singh

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

aaronschwartz avatar May 03 '17 20:05 aaronschwartz

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. :(

rachel-singh avatar May 03 '17 20:05 rachel-singh

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?

aaronschwartz avatar May 03 '17 20:05 aaronschwartz

no sorting and filter props don't change

rachel-singh avatar May 03 '17 21:05 rachel-singh

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.

aaronschwartz avatar May 03 '17 21:05 aaronschwartz

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

rachel-singh avatar May 03 '17 21:05 rachel-singh

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

aaronschwartz avatar May 03 '17 21:05 aaronschwartz

I see. Let me give that a try.

rachel-singh avatar May 03 '17 21:05 rachel-singh

That seems to work. Thanks for your help.

rachel-singh avatar May 03 '17 23:05 rachel-singh