Duplicated
Last Updated: 06 Apr 2020 16:03 by ADMIN
Simon
Created on: 06 Apr 2020 07:20
Category: UI for Blazor
Type: Bug Report
0
Crash when filtering TreeView

Using your tree view live sample, filter the list with "1", then try to expand "Test1" - crash.

I would expect "Test1" to apprear in the list but without the expandable icon or the expandable icon does nothing when clicked


Duplicated
This item is a duplicate of an already existing item. You can find the original item here:
1 comment
ADMIN
Marin Bratanov
Posted on: 06 Apr 2020 16:03

Hi Simon,

This is a manifestation of the following issue: https://feedback.telerik.com/blazor/1448696-changing-treeview-data-source-to-one-with-fewer-levels-can-cause-an-exception-cannot-access-a-disposed-object

Here's the workaround applied to the code from the demo:

<div class="k-card ">
    <div class="k-card-header">
        <TelerikTextBox Value="@FilteredValue" Label="Filter the TreeView" 
                        ValueChanged="@( (string s) => ChangeValues(s) )"></TelerikTextBox>
    </div>
    <div class="k-card-body">
        <TelerikTreeView Data="@FlatData">
            <TreeViewBindings>
                <TreeViewBinding ParentIdField="ParentIdValue"></TreeViewBinding>
            </TreeViewBindings>
        </TelerikTreeView>
    </div>
</div>

@code {
    public class TreeItem
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public int? ParentIdValue { get; set; }
        public bool HasChildren { get; set; }
        public bool Expanded { get; set; }
    }

    public string FilteredValue { get; set; }
    public IEnumerable<TreeItem> FlatData { get; set; }

    async Task ChangeValues(string userInput)
    {
        //workaround - remove it to see the actual error
        foreach (TreeItem item in FlatData)
        {
            item.Expanded = false;
        }
        StateHasChanged();
        await Task.Delay(300);//awaits the animation that will hide the nodes we just collapsed so their elements get properly disposed



        var allItems = LoadFlatData();
        FilteredValue = userInput;
        //prepare a data source based on the desired filter
        var matchedItems = allItems.Where(item => item.Text.Contains(FilteredValue));
        var parentItems = allItems.Where(item => matchedItems.FirstOrDefault(matched => matched.ParentIdValue == item.Id) != null);

        //set the new data to the treeview
        FlatData = matchedItems.Union(parentItems).ToList();

        //update the UI
        StateHasChanged();
    }

    protected override void OnInitialized()
    {
        FlatData = LoadFlatData();
    }

    private IEnumerable<TreeItem> LoadFlatData()
    {
        List<TreeItem> items = new List<TreeItem>();

        for (int i = 1; i <= 4; i++)
        {
            items.Add(new TreeItem()
            {
                Id = i,
                Text = "Test" + i,
                ParentIdValue = null,
                HasChildren = i < 3,
                Expanded = true
            });
        }

        for (int i = 5; i < 15; i++)
        {
            items.Add(new TreeItem()
            {
                Id = i,
                Text = "Test" + i,
                ParentIdValue = (int?)Math.Round((decimal)(i / 5))
            });
        }

        return items;
    }

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.