table icon indicating copy to clipboard operation
table copied to clipboard

Dynamically calculate pageCount when using manualPagination

Open vinnymac opened this issue 6 years ago • 9 comments

Using v7?

Thanks for using the beta version of React Table v7! We're very excited about it.

Describe the bug I am using v7.0.0-rc15 with both useSortBy and usePagination hooks. Mostly everything is working exactly as I would expect and I am very happy with how this project is turning out so thank you for it!

However, I wanted to tell you / ask about an issue I ran into when in manualPagination mode. I have custom controls for paginating and controlling page size over a server, when I update the pageSize via setPageSize or change the page via gotoPage I would expect the pageCount to be re-calculated. I see that we have to explicitly pass in our own pageCount whenever we use both manualPagination and are calling useTable({}). Since pageCount doesn’t appear to update when I update the page or size, I run into bugs, such as react-table thinking their are less pages than their actually are.

I can work around these issues by relying on a Ref, and dynamically updating the pageCount between renders, however it’d be much cleaner/more convenient if the API could optionally make pageCount a function during render. For example:

If I was able to do the following:

const {
  pageCount,
} = useTable({
  initialState: { pageIndex: defaultPageIndex, pageSize: defaultPageSize },
  manualPagination: true,
  pageCount: (pageSize: number) => Math.ceil(totalRows / pageSize)
});

console.log(pageCount); // Up-to-date page count

Now react-table wouldn’t have to update it for me, and I’d have the ability to have better control over the pageCount when I was using manualPagination.

If this is already supported by something please let me know. Maybe I am missing something in the docs that I can’t find, or possibly just am misusing this setup.

To Reproduce Steps to reproduce the behavior:

  1. Make a new table with usePagination
  2. Set manualPagination to true and set a pageCount of 1000 and a pageSize of 50
  3. Update pageSize to 25
  4. See pageCount stay at 20

Expected behavior Given the example above I’d expect to be able to have the pageCount easily update to 40.

Codesandbox! Use a new react-table codesandbox to reproduce the issue.

I’ll try to put together a sandbox and update this issue showing more details, but I think it’s pretty straightforward from the steps I wrote out.

Desktop (please complete the following information):

  • OS: macOS 10.15
  • Browser: Chrome/Safari/Firefox
  • Version: Latest

Additional context Sorry if this is a non-issue or is already documented but I tried without success to find anything else about this, it is a beta after all. Perhaps this will help others when they are searching for a similar solution.

vinnymac avatar Feb 04 '20 13:02 vinnymac

As you are manually controlling pagination the only data that is fetched is the first page. So react-table never knows how many rows there are in total. That's why you need to update the pageCount manually after you fetched new data. Once you update the pageCount, your page calculation is also done right.

Follow this example: https://codesandbox.io/s/github/tannerlinsley/react-table/tree/master/examples/pagination-controlled.

zebrahondje avatar Feb 05 '20 13:02 zebrahondje

@zebrahondje The approach used in that example isn’t unlike the one I arrived at. If we allowed pageCount to be a function however it would remove the need entirely for any of this overhead, outside the API that react table offers.

vinnymac avatar Feb 05 '20 14:02 vinnymac

A function to calculate the pageCount needs two variables: totalRows and pageSize. pageSize won't be an issue, but where would you get totalRows from? That needs to come from the data you're fetching. So basically, you're ending up doing exactly the same thing except you're passing a different prop.

zebrahondje avatar Feb 05 '20 15:02 zebrahondje

That is fair, you'd need the total in manualPagination mode, but the total could be a required argument when it was in that mode.

In any case, this was more meant as a recommendation for what I see as a common use case. The approach in the example above as well as the one I put together work just fine. If it isn't a common case and others don't see the value you in it, I certainly wouldn't want to add scope and increase the complexity of this project.

vinnymac avatar Feb 05 '20 16:02 vinnymac

Great discussion as we keep working on things. Thanks!

tannerlinsley avatar Feb 14 '20 20:02 tannerlinsley

The issue is that sometimes we don't know the pageCount. But instead we know only a totalRowCount. And as this done there's an issue with pageSize being a return value of useTable hook, but to calculate pageCount from totalRowCount we need the pageSize.

There're two possible solutions: 1) allow pageCount to be a function (pageSize:number)=>number or add a new property totalRowCount, so user can either specify pageCount or totalRowCount and the useTable can calculate pageCount internally.

Dema avatar Dec 07 '20 11:12 Dema

This tricky concept can also be used to display the correct information as it subtracts and maintains the total rows count concept :)

<span className="display-information">
	Showing
	<span className="mx-1 font-semibold">
		{ pageIndex * pageSize + 1 }-
		{ ( pageIndex + 1 ) * pageSize -
			( pageSize - page.length ) }
	</span>
	of{ ' ' }
	{ controlledPageCount * pageSize -
		( pageSize - page.length ) }{ ' ' }
	items
</span>

shivapoudel avatar Dec 31 '20 22:12 shivapoudel

When I'm passing pageCount as a function, the canPreviousPage and canNextPage are always returned as false. Am I doing something wrong here?

subhamayd2 avatar Jul 15 '21 11:07 subhamayd2

@Dema 's explanation lays bare the problem with this API: it contains a circular reference blooper.

It wants us to compute pageCount manually, but that depends on pageSize, which is stored internally.

@tannerlinsley Why close the thread? I understand you are busy, but that does not render this issue invalid. Can you re-open it?

Ideal Solution

Instead of pageCount, useTable should take totalRowCount (or somesuch). Then it can easily compute pageCount and friends for us, without us having to store pageSize separately.

Temporary Workaround

Until that API change happens:

Compute pageCount, canPreviousPage, canNextPage, pageOptions manually, after calling useTable.

E.g.:

let { pageSize, pageCount } = tableInstance.state;
pageCount = manualPagination ? Math.ceil(totalCount / pageSize) : pageCount;

More can be found in the usePagination hook.

Domiii avatar Aug 08 '21 10:08 Domiii