How to bind complex data in Blazor Pivot Table
24 Aug 20265 minutes to read
The Blazor Pivot Table component requires flat data for binding. Complex data, such as nested objects or hierarchical structures, must be converted into a flat structure before it can be assigned to the Pivot Table using the DataSource property within PivotViewDataSourceSettings. Field names are then mapped to rows, columns, and values under PivotViewDataSourceSettings to display the data effectively.
Sample data and flat-data conversion
The following example builds a list of ExpandoObject items that contains a nested ShipDetails object, and then flattens that structure using the ConvertToFlatData helper. The helper iterates each top-level item; for every child whose value is an ExpandoObject, it copies the nested key/value pairs up to the parent, producing a single-level record that the Pivot Table can consume. The flattened list is then assigned to the DataSource property.
@using Syncfusion.Blazor.PivotView
@using System.Dynamic
<SfPivotView TValue="ExpandoObject">
<PivotViewDataSourceSettings DataSource="@Orders">
<PivotViewColumns>
<PivotViewColumn Name="OrderID"></PivotViewColumn>
<PivotViewColumn Name="ShipName"></PivotViewColumn>
</PivotViewColumns>
<PivotViewRows>
<PivotViewRow Name="CustomerID"></PivotViewRow>
</PivotViewRows>
<PivotViewValues>
<PivotViewValue Name="OrderDate"></PivotViewValue>
<PivotViewValue Name="Freight" Type="SummaryTypes.Max"></PivotViewValue>
</PivotViewValues>
</PivotViewDataSourceSettings>
</SfPivotView>
@code {
public List<ExpandoObject> Orders { get; set; } = new List<ExpandoObject>();
protected override void OnInitialized()
{
Orders = Enumerable.Range(1, 75).Select((x) =>
{
dynamic d = new ExpandoObject();
d.OrderID = 1000 + x;
d.CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)];
d.Freight = (new double[] { 2, 1, 4, 5, 3 })[new Random().Next(5)] * x;
d.OrderDate = (new DateTime[] { new DateTime(2010, 11, 5), new DateTime(2018, 10, 3), new DateTime(1995, 9, 9), new DateTime(2012, 8, 2), new DateTime(2015, 4, 11) })[new Random().Next(5)];
d.ShipCountry = (new string[] { "USA", "UK" })[new Random().Next(2)];
d.Verified = (new bool[] { true, false })[new Random().Next(2)];
d.ShipDetails = new ExpandoObject();
d.ShipDetails.ShipName = (new string[] { "ALTEN-I", "BONTER", "GITOR", "MELON", "CRUISE" })[new Random().Next(5)];
d.ShipDetails.ShipId = 25000 + x;
return d;
}).Cast<ExpandoObject>().ToList<ExpandoObject>();
Orders = ConvertToFlatData(Orders);
}
public List<ExpandoObject> ConvertToFlatData(List<ExpandoObject> Data)
{
List<ExpandoObject> flatData = new List<ExpandoObject>();
foreach (var item in Data)
{
dynamic obj = new ExpandoObject();
foreach (var child in item)
{
if (child.Value.ToString() == "System.Dynamic.ExpandoObject")
{
var dynamicObj = (IDictionary<string, object>)child.Value;
foreach (var New in dynamicObj)
{
var field = New.Key;
var fieldvalue = dynamicObj[field];
((IDictionary<String, Object>)obj).Add(field, fieldvalue);
}
}
else
{
var field = child.Key;
var dynamicObj = (IDictionary<string, object>)item;
var fieldvalue = dynamicObj[field];
((IDictionary<String, Object>)obj).Add(field, fieldvalue);
}
}
flatData.Add(obj);
}
return flatData;
}
}