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

[DataGrid] Add methods to save and restore scroll position #5071

Open
2 tasks done
joe0BAB opened this issue Jun 1, 2022 · 8 comments
Open
2 tasks done

[DataGrid] Add methods to save and restore scroll position #5071

joe0BAB opened this issue Jun 1, 2022 · 8 comments
Labels
component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request

Comments

@joe0BAB
Copy link

joe0BAB commented Jun 1, 2022

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Summary 💡

There already is an API to export/restore parts of the Datagrid state (related to #820). It would be awesome to extend this and add scroll position. As a current workaround we hook into rowScroll event to persist scroll position but restoring it is a bit hacky:

useEffect(() => {
    setTimeout(() => {
      apiRef.current.scroll({
        top: scrollPosition,
        left: 0,
      });
    });
  }, [apiRef.current]);

Without the setTimeout hack we get
image

Examples 🌈

No response

Motivation 🔦

We want to be able to restore Datagrid scroll position. Ideally by using initialState prop.

Maybe there's a workaround until then that's cleaner as the hack suggested above.

Order ID 💳 (optional)

41693

@joe0BAB joe0BAB added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jun 1, 2022
@m4theushw
Copy link
Member

Could you provide a reproduction demonstrating the error you see? With static rows it works, but with rows loaded from the server it didn't, but no error.

We do can allow to save the scroll position and restore it passing the value in the initialState prop. But first I would like to understand the error message, it might be related to #4674

@m4theushw m4theushw added component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jun 1, 2022
@AndreSilva1993
Copy link

AndreSilva1993 commented Jun 1, 2022

The issue you mentioned has the exact same error that we're experiencing but I can try to get you a quick reproducible example. We're just calling apiRef.current.scroll inside an useEffect (which has apiRef.current as a dependency) so it should be easily reproducible on a codesandbox. Tomorrow we'll post something!

@AndreSilva1993
Copy link

There you go - https://codesandbox.io/s/compassionate-kirch-bpxoz1?file=/src/App.tsx

@m4theushw Is there something wrong in our usage? I'd assume that waiting for apiRef.current should be enough.

@m4theushw
Copy link
Member

@AndreSilva1993 your usage is correct for any React component, but the DataGrid doesn't work with it. When the effect runs it only means that apiRef is available, however, windowRef will only become available after some other state changes.

@AndreSilva1993
Copy link

AndreSilva1993 commented Jun 3, 2022

@m4theushw Then how would you suggest for us to deal with us? I feel like waiting for windowRef is a bit hack-ish as it's an internal implementation.

@HansBrende
Copy link

@m4theushw

We do can allow to save the scroll position and restore it passing the value in the initialState prop.

I don't see this anywhere in the initialState typings... did you mean you currently do allow, or you do not allow yet but can allow later?

@noghartt
Copy link

Hey guys, is there any update on that? Any method to store and restore the last scrolled position on datagrid?

@sibelius
Copy link

any workaround ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! new feature New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants