How to Group the Column Chooser Items
16 Aug 202311 minutes to read
The GridColumnChooserItemGroup component helps to segregate the column chooser items as group. You can define column’s group name by using the Title property of GridColumnChooserItemGroup directive.
The following code example demonstrates the default column chooser items as group.
@using Syncfusion.Blazor.Grids
@using Syncfusion.Blazor.Buttons
<SfGrid @ref="grid" TValue="OrdersDetails" DataSource="@GridData" ShowColumnChooser="true" Toolbar="@( new List<string>() { "ColumnChooser"})" AllowPaging="true">
<GridColumnChooserSettings>
<Template>
@{
var ContextData = context as ColumnChooserTemplateContext;
}
@if (ShouldRenderGroup("Order Details", ContextData.Columns))
{
<GridColumnChooserItemGroup Title="Order Details">
@foreach (var column in GetGroupColumns("Order Details", ContextData.Columns))
{
<GridColumnChooserItem Column="column"></GridColumnChooserItem>
}
</GridColumnChooserItemGroup>
}
@if (ShouldRenderGroup("Ship Details", ContextData.Columns))
{
<GridColumnChooserItemGroup Title="Ship Details">
@foreach (var column in GetGroupColumns("Ship Details", ContextData.Columns))
{
<GridColumnChooserItem Column="column"></GridColumnChooserItem>
}
</GridColumnChooserItemGroup>
}
@if (ShouldRenderGroup("Date Details", ContextData.Columns))
{
<GridColumnChooserItemGroup Title="Date Details">
@foreach (var column in GetGroupColumns("Date Details", ContextData.Columns))
{
<GridColumnChooserItem Column="column"></GridColumnChooserItem>
}
</GridColumnChooserItemGroup>
}
</Template>
<FooterTemplate>
@{
var ContextFooterData = context as ColumnChooserFooterTemplateContext;
var visibles = ContextFooterData.Columns.Where(x => x.Visible).Select(x => x.HeaderText).ToArray();
var hiddens = ContextFooterData.Columns.Where(x => !x.Visible).Select(x => x.HeaderText).ToArray();
}
<SfButton IsPrimary="true" OnClick="@(async () => {
await grid.ShowColumnsAsync(visibles);
await grid.HideColumnsAsync(hiddens); })">
Submit
</SfButton>
<SfButton @onclick="@(async () => await ContextFooterData.CancelAsync())">Abort</SfButton>
</FooterTemplate>
</GridColumnChooserSettings>
<GridColumns>
<GridColumn Field=@nameof(OrdersDetails.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" IsPrimaryKey="true" Width="120"></GridColumn>
<GridColumn Field=@nameof(OrdersDetails.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>
<GridColumn Field=@nameof(OrdersDetails.OrderDate) HeaderText="Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field=@nameof(OrdersDetails.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field=@nameof(OrdersDetails.ShippedDate) HeaderText="Shipped Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="150"></GridColumn>
<GridColumn Field=@nameof(OrdersDetails.ShipCountry) HeaderText="Ship Country" Visible="false" Width="150"></GridColumn>
<GridColumn Field=@nameof(OrdersDetails.ShipCity) HeaderText="Ship City" Visible="false" Width="150"></GridColumn>
</GridColumns>
</SfGrid>
@code
{
public List<OrdersDetails> GridData { get; set; }
SfGrid<OrdersDetails> grid { get; set; }
IDictionary<string, string[]> groups = new Dictionary<string, string[]>()
{
{ "Order Details", new string[] { "OrderID", "CustomerID", "Freight" } }, { "Ship Details", new string[]{ "ShipCountry", "ShipCity" } }, {"Date Details", new string[]{"OrderDate", "ShippedDate"}}
};
private GridColumn GetColumn(string field, List<GridColumn> columns)
{
GridColumn column = null;
if (columns.Any(x => { column = x; return x.Field == field; }))
{
return column;
}
return null;
}
private bool ShouldRenderGroup(string title, List<GridColumn> columns)
{
return groups[title].Any(x => columns.Any(y => y.Field == x));
}
private List<GridColumn> GetGroupColumns(string title, List<GridColumn> columns)
{
return columns.Where(x => groups[title].Contains(x.Field)).ToList();
}
protected override void OnInitialized()
{
GridData = Enumerable.Range(1, 75).Select(x => new OrdersDetails()
{
OrderID = 1000 + x,
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
Freight = 2.1 * x,
OrderDate = DateTime.Now.AddDays(-x),
ShippedDate = DateTime.Now.AddDays(+x),
ShipCountry = (new string[] { "Denmark", "Brazil", "Germany", "Austria" })[new Random().Next(4)],
ShipCity = (new string[] { "Berlin", "Madrid", "Marseille" })[new Random().Next(3)]
}).ToList();
}
public class OrdersDetails
{
public int? OrderID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
public double? Freight { get; set; }
public DateTime? ShippedDate { get; set; }
public string ShipCountry { get; set; }
public string ShipCity { get; set; }
}
}