Declined
Last Updated: 27 Apr 2021 12:13 by ADMIN
ben
Created on: 20 Apr 2021 19:45
Category: TreeList
Type: Bug Report
0
Tree List - Expanded State Not Restored from Local Storage

Issue - While restoring the tree list state from local storage any items that were expanded are not re-expanded

Repo - https://github.com/benhysell/BlazorGridPagingIssue

Example Inspired By - https://docs.telerik.com/blazor-ui/components/treelist/state#save-and-load-treelist-state-from-browser-localstorage

Steps to Reproduce

Expected behavior - Elements that had been expanded would be expanded again.

Details

Following the example https://docs.telerik.com/blazor-ui/components/treelist/state#save-and-load-treelist-state-from-browser-localstorage I copied the code into my application, however I wanted to save which items were expanded, so I commented out:

 state.ExpandedItems = null;

Looking at the resulting entries in local storage, it appears the ExpandedItems list is properly saved to local storage.

I added some logging to the OnStateInitHandler so I could see what was happening on load, my desired result was to ensure:

  • If there is nothing in local storage, collapse all items
  • If someone had stored some information in local storage use it

It appears everything is going out to local storage, and coming back in without issue, however the state of the expanded items is not reflected in the control.

 

1 comment
ADMIN
Hristian Stefanov
Posted on: 27 Apr 2021 12:13

Hello Ben,

I reproduced the described behavior and used the provided code on my machine. To achieve the desired result, there are two options:

  • Intermediate additional filtration - this is necessary because the Equals method works with references. In your case, to achieve that, all you need to do is add the below code block to the TreeListExample.razor.cs file starting from row 58.
    var expandedIds = localStorage.ExpandedItems.Select(x => x.Id);
    var expandedItems = TreeListItems.Where(x => expandedIds.Contains(x.Id)).ToList();
    localStorage.ExpandedItems = expandedItems;
    args.TreeListState = localStorage;
  • Overriding the Equals method - thus you will be able to avoid the reference comparison.

In the attached file, I have used the first way. There you can see the example you provided with the added required lines of code in the TreeListExample.razor.cs.

In the meantime, I am marking the portal status of this thread as "Declined" since the described behavior is not an actual bug and the desired result could be achieved with the current setup of the component, using one of the above-mentioned approaches.

I hope this helps and if you have any other questions, let me know.

Regards,
Hristian Stefanov
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Attached Files: