Completed
Last Updated: 08 Sep 2020 15:35 by ADMIN
Release 2.17.0
Christopher
Created on: 11 Aug 2020 17:19
Category: TreeView
Type: Bug Report
4
The TreeView does not update when providing a new data source until you collapse and expand the node whose children were modified

The issue manifests both when

  • using .Add() or .Remove() on an ObservableCollection
  • creating a new List<T>(updatedData)

Happens in 2.14.1 first.

*** Thread created by admin on customer behalf ***

2 comments
ADMIN
Marin Bratanov
Posted on: 23 Aug 2020 07:03

Hello,

Generally, creating a new collection for its Data will change its reference, so the framework will call the OnParametersSet method of the treeview and it will update. The alternative is to use an ObservableCollection and its Add(), Remove() and Clear method().

The bug this page tracks is that neither of these approaches works.

A workaround could be to toggle its visibility so it re-renders completely anew with the new data:

@using System.Collections.ObjectModel

<TelerikButton OnClick="@AddChildClick">Add Child</TelerikButton>

@if (TreeViewVisible)
{
    <TelerikTreeView Data="@ProductTypesFlatData">
        <TreeViewBindings>
            <TreeViewBinding ParentIdField="ParentIdValue" ExpandedField="Expanded"></TreeViewBinding>
        </TreeViewBindings>
    </TelerikTreeView>
}

@code{
    bool TreeViewVisible { get; set; } = true;
    async Task RepaintTree()
    {
        TreeViewVisible = false;
        await Task.Delay(30);
        TreeViewVisible = true;
        StateHasChanged();
    }

    private ObservableCollection<TreeItem> ProductTypesFlatData { get; set; }

    private int _childCounter = 1;

    protected override void OnInitialized()
    {
        ProductTypesFlatData = new ObservableCollection<TreeItem>();
        ProductTypesFlatData.Add(new TreeItem() { Expanded = true, HasChildren = true, Icon = IconName.Folder, Id = "1", ParentIdValue = null, Text = "Parent1" });
        AddChild();
        AddChild();
        AddChild();
    }

    private async Task AddChildClick()
    {
        AddChild();
        await RepaintTree();
    }

    private void AddChild()
    {
        ProductTypesFlatData.Add(new TreeItem() { Expanded = false, HasChildren = false, Icon = IconName.Gear, Id = $"1.{_childCounter.ToString()}", ParentIdValue = "1", Text = $"Child{_childCounter}" });
        _childCounter++;
    }

    public class TreeItem
    {
        public string Id { get; set; }
        public string Text { get; set; }
        public string ParentIdValue { get; set; }
        public bool HasChildren { get; set; }
        public string Icon { get; set; }
        public bool Expanded { get; set; }
    }
}

 

Regards,
Marin Bratanov
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

How PK
Posted on: 23 Aug 2020 04:21

Anyway to refresh the TreeView manually?