table icon indicating copy to clipboard operation
table copied to clipboard

making defaultPageSize or pageSize trigger a re-render to update number of rows

Open scottclayton opened this issue 9 years ago • 4 comments

Hello!

I love this package!

Question though: on a few occasions I've run into the issue where I want the defaultPageSize or pageSize to be determined by the length of the data I'm feeding into the component. However, upon changing the data, the number of rows in the ReactTable will not change. E.g. If I initially feed in an array with 3 items and then give it 2 items, there will be 1 blank row if I use the data.length as my defaultPageSize or pageSize props. If I'm using defaultPageSize and I initially feed in an array with 2 items and then give it 3 items, there will still only be 2 rows, and thus the third item isn't displayed at all. However, pageSize does change the number of rows to three in that instance, but then of course if I then feed in a data set with 2 items, there will be an empty third row.

Any idea how to force a change to the number of rows?

Would lifecycle.js benefit from oldState.pageSize !== newState.PageSize as an additional // Props that trigger a data update item?

I have been calling my components like this:

<ReactTable className="-striped -highlight" data={vendors} columns={vendorColumns} defaultPageSize={vendors.length} pageSize={vendors.length} noDataText="No Vendor Listings Found" showPageJump={false} showPagination={false} showPageSizeOptions={false} />

An advantage of having the defaultPageSize or pageSize props trigger a change in the number of rows is that it could also allow for a dynamic number of rows if some calculations are done based on things like window.innerHeight, which would be super cool!

scottclayton avatar Mar 04 '17 18:03 scottclayton

When I was driving home just now, I realized that the component itself is probably re-rendering, as I am passing in new data with the changes, and new data should force a re-render. That makes me think that oldState.pageSize !== newState.PageSize probably wouldn't be of benefit in this case. The issue is to do with rows instead.

Digging around and experimenting some more, minRows is a prop! minRows={data.length} seems to be the way to handle this sort of situation. That would make more sense, as I'm not paginating in the above-examples, yet I was trying to pass props that handled the page size. Neat! There isn't a maxRows prop, but the number of rows will grow and shrink based on the value going into minRows; unlike with pageSize prop, going from a length of 3 to a length of 2 will change the number rows to 2 rather than leaving a blank row. I don't need to even specify defaultPageSize or pageSize props. Neat!

I will close this issue!

I can probably accomplish the responsive number of rows based on windows.innerHeight idea if I have pagination and use some combination of the defaultPageSize and pageSize props too, so good news all around!

scottclayton avatar Mar 05 '17 07:03 scottclayton

In case anyone is looking at this and interested, a simple way of producing a responsive number of rows could be something like:

pageSize={Math.floor((window.innerHeight - 190) / 33)}, where 190 is the height of the elements on the page to have visible when the chart is displayed, and 33 is the height of each row of the chart. It isn't completely responsive, as there isn't a listener or alternative on the window object, so if the user changes their window's dimensions while viewing the rendered component, the number of rows visible at a time will be the same as before the dimensions changed, but it will change as the component is re-rendered (e.g. if more data is fed in). It's not perfect, but it works fairly well.

scottclayton avatar Mar 05 '17 08:03 scottclayton

Hi The defaultPageSize has a default value of 20 so even when I don't mention it the table always has at least 20 rows. How did you override it?

achowdhury7 avatar Aug 16 '17 12:08 achowdhury7

@achowdhury7

To be honest I forget and I haven't built new things with this package in 4 months, but I suspect it was a combination of the minRows, and pageSize props, rather than using defaultPageSize.

I had this tablePageSize helper function to make it parameterized to the height of the browser window:

export const tablePageSize = (morePadding = 0) => Math.floor((window.innerHeight - 100 - morePadding) / 33)

Then most of my code looks something like this for my ReactTable implementation:

        <ReactTable
          data={data}
          columns={dataColumns}
          className="-striped -highlight"
          noDataText="No departments found"
          minRows={data.length || 3}
          pageSize={tablePageSize()}
          showPageJump={false}
          showPagination={data.length > (tablePageSize())}
          showPageSizeOptions={false}
          page={data.length <= tablePageSize() ? 0 : undefined}
        />

Whatever the case, I recommend playing around with all the props though and determining how they each affect it.

Good luck!

scottclayton avatar Aug 27 '17 22:08 scottclayton