Restoring table state does not restore sorting if a default sortField and sortOrder are set.
I'm submitting a ... (check one with "x")
[x] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35
Stackblitz (Bug Reports)
I have taken the default state restoring example from this page: link
And all I have changed is that I have added a default sortField and sortOrder (line 4.):
sortField="name" [sortOrder]="-1"
Here is the edited stackblitz: https://stackblitz.com/edit/primeng-tablestate-demo-zyy7qj
Current behavior
- I enter the page for the first time and see a default sortField/Order set. (e.g. 'name')
- I change the sorting to 'country'.
- I filter by name 'james'
- I refresh the page
- The table will be sorted by the original/default sortField ('name') again.
Expected behavior
- I enter the page for the first time and see a default sortField/Order set. (e.g. 'name')
- I change the sorting to 'country'.
- I filter by name 'james'
- I refresh the page
- The table will be sorted by the last used sortField ('country').
Minimal reproduction of the problem with instructions
If you remove this:
sortField="name" [sortOrder]="-1"
on line 4 in the stackblitz and the view refreshes, it will automatically jump back to sorting by country, if you have sorted by country before.
This shows that the default sortField overrules the restored sorting field.
What is the motivation / use case for changing the behavior? I want to have an initial sortField and sortOrder so that the visible filter matches my lazy loaded table content (I am sorting by a specific field by default and to help the user understand what's going on, I want this filter to be highlighted from the beginning, this - afaik - is only possible by using the default sorting field/order) BUT I want this default behavior to be overruled in case the table was restored from an earlier state, because the user has already changed filters/sorting
-
Angular version: 12
-
PrimeNG version: 12
I am working on a workaround (by setting the initial ordering manually) and I stumbled over a question: Let's say I am using lazy loading but i have lazyLoadOnInit=false. Then I am assuming the (onRestore) function will be called at some point, but how do I know - beforehand - if it WON'T be called eventually (because it is the user's first time on the page for example) - so that I can trigger the initial loading manually (e.g. in ngOnInit of the component that contains the table)?
Have the same issue. Ended up in doing something on my own...
I have the same problem. Managed to make it work with a custom workaround, but would be great to be able to use default sort field and sort order with state storage.
Hi,
So sorry for the delayed response! Improvements have been made to many components recently, both in terms of performance and enhancement. Therefore, this improvement may have been developed in another issue ticket without realizing it. You can check this in the documentation. If there is no improvement on this, can you reopen the issue so we can include it in our roadmap? Please don't forget to add your feedback as a comment after reopening the issue. These will be taken into account by us and will contribute to the development of this feature. Thanks a lot for your understanding!
Best Regards,
@ThomasPrikrylBE can You please verify it? I think it still exists in 14.2.1 version
Have the same issue in 14 version(
If I have [sortField]="'someField'" - restoring sortField from state is not working
I can confirm this is still an issue.

It is related to the ngOnChanges function. It detects if a value is used as property. Then it restores the state, which will load the previous order of the table. Some lines later it will just overwrite the sortMeta if the multiSortMeta input was set.
So it might be better to restore the state AFTER everything else was set?
Same issue in 17 version.
same here also on the latest v17
Still facing the issue in the latest v17
Hi,
So sorry for the delayed response! Improvements have been made to many components recently, both in terms of performance and enhancement. Therefore, this improvement may have been developed in another issue ticket without realizing it. You can check this in the documentation. If there is no improvement on this, can you open a new issue so we can include it in our roadmap?
Thanks a lot for your understanding! Best Regards,