Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep Angular Material table state after Component redirection

I have a TableComponent with an Angular Material table that contains some links to other components.

Second page of the table

When I am on the second page of the table (Image above) and I click on the link, routerLink redirects to DetailComponent. Once I am redirected, if I press the back button or I execute location.back() I go back to TableComponent, but the table is located on the first page, instead of the one I was before.

First page of the table

Is there a way to keep the previous state of the table, after redirections between components?

Thanks in advance!

like image 535
Iñigo Avatar asked Mar 06 '26 23:03

Iñigo


1 Answers

When you're redirecting between components, their content is destroyed, so there is no way for a destroyed component to remember anything ;) But you can store the information about the page index somewhere else in the application (service or store if you're using Redux).

So you can save the page index before redirection i.e. using service or even better just update service after every page change.

Then you can get the index and use pageIndex property of MatPaginator as described in documentation: https://material.angular.io/components/paginator/api to set correct page index after getting back to this page. Let me know if you need any more help with that.

like image 91
Patryk Łucka Avatar answered Mar 09 '26 19:03

Patryk Łucka