Hello,
I am seeing an issue with the DataGrid when trying to use self referencing hierarchi and it's something to do with the class that is the type of the Datagrid.
@using EsioBudget.Application.Budget.DataTransferObjects
@using MediatR
@using Microsoft.Extensions.Localization
@if (_model == null)
{
<CircularProgressBar></CircularProgressBar>
}
else
{
<RadzenDataGrid @ref="_grid" AllowFiltering="true" AllowSorting="true" AllowColumnResize="true"
Data="@_model.Items" RowRender="@RowRender" LoadChildData="@LoadChildData" TItem="Dummy"
RowCollapse="@(args => _grid.ColumnsCollection.ToList().ForEach(c => c.ClearFilters()))">
<Columns>
<RadzenDataGridColumn Title="Employee">
<Template Context="data">
@data.Name
</Template>
</RadzenDataGridColumn>
</Columns>
</RadzenDataGrid>
}
@code {
[Parameter] public IEnumerable<MetricLineData> MetricLines { get; set; }
[Parameter] public IEnumerable<IntervalData> Intervals { get; set; }
[Parameter] public bool ShowHeaders { get; set; }
[Inject] public required IMediator Mediator { get; set; }
[Inject] public required NotificationService NotificationService { get; set; }
[Inject] public required IStringLocalizer<AppResources> Localizer { get; set; }
private class Dummy(string name, IEnumerable<Dummy> subItems)
{
public string Name { get; set; } = name;
public IEnumerable<Dummy> SubItems { get; set; } = subItems;
}
private class ViewModel(IEnumerable<Dummy> items, IEnumerable<MetricLineData> lines, IEnumerable<IntervalData> intervals)
{
public IEnumerable<Dummy> Items = items;
public IEnumerable<MetricLineData> MetricLines { get; set; } = lines;
public IEnumerable<IntervalData> Intervals { get; set; } = intervals;
}
private ViewModel _model;
private RadzenDataGrid<Dummy> _grid;
protected override async Task OnParametersSetAsync()
{
_model = new ViewModel(
[new Dummy("Item 1", [new Dummy("SubItem1", [])])],
MetricLines,
Intervals
);
}
void RowRender(RowRenderEventArgs<Dummy> args)
{
args.Expandable = args.Data.SubItems.Any();
}
void LoadChildData(DataGridLoadChildDataEventArgs<Dummy> args)
{
args.Data = args.Item.SubItems;
}
}
In this component example, the expanding of the item row works fine when using the "Dummy" class. When I change however the type to use the "MetricLineData" which I made, then the expanding button does not work anymore.
This is the class in question:
public class MetricLineData(string name, List<MetricData> metrics, List<MetricLineData>? submetrics, decimal total)
{
public string Name { get; set; } = name;
public List<MetricData> Metrics { get; set; } = metrics;
public List<MetricLineData>? MetricLines { get; set; } = submetrics;
public decimal Total { get; set; } = total;
public static MetricLineData Map(MetricLine metricLine)
{
return new MetricLineData(
metricLine.Name,
metricLine.Metrics.Select(x => MetricData.Map(x)).ToList(),
metricLine.MetricLines?.Select(x => Map(x)).ToList(),
metricLine.Total
);
}
public MetricData? GetForInterval(IntervalData interval)
{
return Metrics.FirstOrDefault(x => x.Interval.Index == interval.Index);
}
}
I'd like to know what I'm doing wrong with this class that prevents the hierarcy expantion