RadzenTree questions

Wow that's a lot of questions :slight_smile:

I will try to answer as many as I can:

ASP.net app to blazor, i am missing some functionality in the RadzenTree component

The RadzenTree probably won't match your existing ASP.NET tree component API 1:1. Blazor is more of a declarative framework (uses data-binding instead of code-behind) while you seem to be looking for imperative API - adding nodes from code, accessing parent nodes etc.

expand a certain node

One can use the Expanded property to do that.

expand all nodes at once after they are loaded (like _tree.ExpandAllNodes)

The <RadzenTreeLevel> has Expanded property. The following will expand all nodes:

<RadzenTreeLevel TextProperty="CategoryName"
  ChildrenProperty="Products" Expanded="@(data => true)">
  • the node depth (root node =1, subnode = 2, next subnode = 3 and so on)
  • parent node
  • data object of parent node
  • traversal possibility of selected node up to root node (e.g. ParentNode.ParentNode.Name)

Those are indeed not available. We can easily add the node level. However maintaining the node hierarchy in memory is something we are not sure we should do. It has the potential to create memory leaks (a parent has children and the child refers to its parent which creates a loop). Also Blazor does not currently have any API that notifies a parent component that a child is removed or updated.

As a workaround I can suggest one of the following:

  • Create an object hierarchy that has Parent/Child relationship as data. You can use that hierarchy to data-bind the treeview (you are actually suggesting that later in your post). Here is an example:
<RadzenTree Data="@nodes">
   <RadzenTreeLevel Text="@(data => ((DataNode)data).Data.ToString())" 
         ChildrenProperty="Children" 
         HasChildren="@(data => ((DataNode)data).Children != null)" 
    />
</RadzenTree>
@code {
    public class DataNode
    {
        public object Data { get; set; }
        public IEnumerable<DataNode> Children { get; set; }
    }

    DataNode[] nodes = new DataNode[]
    {
        new DataNode
        {
            Data = "Root",
            Children = new [] {
                new DataNode
                {
                    Data = "Child"
                }
            }
        }
    };
}
  • ะพr use your API to find the parent of the current data item - if it is a file - get its parent directory, otherwise query the DB.
1 Like