How to get the current Page Index of the TantStack table
P粉909476457
P粉909476457 2023-09-02 12:11:52
0
1
529
<p>I'm building a table using TantStack and the React Table library. I want to display the current page index in the table footer, like "Page 1 of 8", where "1" means the current page number and "8" is the total number of pages. I can't figure out how to access the current page index from the TantStack table state. </p> <p>It should be placed between these buttons: </p> <pre class="brush:php;toolbar:false;"><Button variant="outline" size="sm" onClick={() => table.previousPage()} disabled={!table.getCanPreviousPage()} > Previous </Button> <Button variant="outline" size="sm" onClick={() => table.nextPage()} disabled={!table.getCanNextPage()} > Next </Button></pre></p>
P粉909476457
P粉909476457

reply all(1)
P粉187160883

You need to store the page index in a state variable and pass the setState method to the tanstack table to set the current page index. You can try the following code:

const [state, setState] = useState(table.initialState);

  const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
    pageIndex: 0,
    pageSize: 10
  });

  const pagination = useMemo(
    () => ({
      pageIndex,
      pageSize
    }),
    [pageIndex, pageSize]
  );

  // override the state managers for the table
  table.setOptions(prev => ({
    ...prev,
    onStateChange: setState,
    state: {
      ...state,
      pagination,
    },
    onPaginationChange: setPagination,
    pageCount: Math.ceil(numOfRows / pageSize)
  }));
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!