Welcome to the Blazor feedback portal. We’re happy you’re here! If you have feedback on how to improve the Blazor, we’d love to hear it!>
Thanks for joining our community and helping improve Syncfusion products!
I am not able to dynamically bind a child data to a tree node.
I have a model which has empty child contents. Upon clicking of the parent tree node ,I want to dynamically bind the child contents of it and load the tree view.
This should have for all the tree node.
@page "/RemoteData"
@using Syncfusion.EJ2.Blazor.Navigations
@using Syncfusion.EJ2.Blazor.Lists
@using Syncfusion.EJ2.Blazor.Data
<div class="col-lg-12 control-section">
<div class="control_wrapper">
<div class="col-lg-12 control-section">
<div class="col-lg-6 nested-data">
<div class="content">
<h4>Hierarchical Data</h4>
<EjsTreeView TValue="TreeData">
<TreeViewFieldsSettings DataSource="@TreeDataSource" Id="Code" Text="Name" Selected="Selected" Expanded="Expanded" HasChildren="HasChild" ></TreeViewFieldsSettings>
<TreeViewEvents TValue="TreeData" NodeClicked="ExpandCollapse" ></TreeViewEvents>
</EjsTreeView>
</div>
</div>
</div>
</div>
</div>
@code{
public class TreeData
{
public string Code { get; set; }
public string Name { get; set; }
public bool Expanded { get; set; }
public bool Selected { get; set; }
public List<TreeData> Child { get; set; }
public bool HasChild { get; set; }
}
void ExpandCollapse(NodeClickEventArgs args)
{
var InidivisualChild = new List<TreeData>()
{
new TreeData { Code = "CHN", Name = "China" },
new TreeData { Code = "IND", Name = "India" },
new TreeData { Code = "JPN", Name = "Japan" },
};
var Child2 = new List<TreeData>()
{
new TreeData { Code = "USA", Name = "United States of America" + args.Node.ID, Selected = true },
new TreeData { Code = "CUB", Name = "Cuba" + args.Node.ID },
new TreeData { Code = "MEX", Name = "Mexico" + args.Node.ID },
};
TreeDataSource.Where(e => e.Name == args.Node.ID).Select(e1 => { e1.Child = InidivisualChild; return e1; });
TreeDataSource.Where(e => e.Name == args.Node.ID).Select(e1 => { e1.HasChild= true; return e1; });
// StateHasChanged();
}
List<TreeData> TreeDataSource = new List<TreeData>();
protected override void OnInitialized()
{
TreeDataSource.Add(new TreeData
{
Code = "NA",
Name = "North America",
Expanded = true,
HasChild=true
});
TreeDataSource.Add(new TreeData
{
Code = "AF",
Name = "Africa",
HasChild=true
});
TreeDataSource.Add(new TreeData
{
Code = "AS",
Name = "Asia",
HasChild=true,
Child = new List<TreeData>()
{
new TreeData { Code = "CHN", Name = "China" },
new TreeData { Code = "IND", Name = "India" },
new TreeData { Code = "JPN", Name = "Japan" },
},
});
}
}
<style>
.control_wrapper {
max-width: 500px;
margin: auto;
border: 1px solid #dddddd;
border-radius: 3px;
}
</style>
In the practical scenario the data will be loaded from a separate class which will have the call to API.