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
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