Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination and activePage prop #60

Closed
vlrmprjct opened this issue Nov 19, 2021 · 4 comments
Closed

Pagination and activePage prop #60

vlrmprjct opened this issue Nov 19, 2021 · 4 comments
Assignees
Labels
question Something is not clear

Comments

@vlrmprjct
Copy link

Hello @joaocarmo ,

just my next question ...

I try to figure out how to set the table initially to a different page.
Lets say the route entry url looks like ../whatever/fancy/page/2 . Now I listen to an activePage param, here the 2 .

Is there a way to handle this ?

@joaocarmo joaocarmo self-assigned this Nov 20, 2021
@joaocarmo joaocarmo added the question Something is not clear label Nov 20, 2021
@joaocarmo
Copy link
Owner

Great question, @vlrmprjct!

You can already control the component through its internal API like:

import { useEffect, useRef } from 'react'
import SmartDataTable from 'react-smart-data-table'
import data from './data.json'

const getPageFromUrlPath = () => {  
  const url = new URL(window.location)
  const urlParts = url.pathname.split('/')
  const page = urlParts[urlParts.length - 1]

  return page ? parseInt(page, 10) : 1
}

const App = () => {
  const tableRef = useRef()

  useEffect(() => {
    if (tableRef?.current) {
      tableRef.current.handleOnPageChange(null, { activePage: getPageFromUrlPath() })
    }
  }, [])

  return (
    <main>
      <h1>App</h1>
      <hr />
      <SmartDataTable
        name="test"
        data={data}
        perPage={10}
        ref={tableRef}
      />
    </main>
  )
}

@vlrmprjct
Copy link
Author

Olá @joaocarmo,

Thank You ! Nice !
I will give that a try and in this case I would decline the PR.

@vlrmprjct
Copy link
Author

Works pretty neat @joaocarmo ,
also with this solution it is possible to create an "external" pagination everywhere in the markup!

Thank You! 👍🏼

@joaocarmo
Copy link
Owner

@vlrmprjct great to know it worked out well for you! Thanks for letting me know.

I've added the information to the proper section in the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Something is not clear
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants