Below, you could see a short code snippet with a sample implementation of a custom column chooser:
<TelerikGrid Data="@MyData"
Pageable="true"
PageSize="10">
<GridToolBar>
<TelerikButton OnClick="@( _ => isModalVisible = true )">Column Chooser</TelerikButton>
</GridToolBar>
<GridColumns>
@foreach (var col in Columns)
{
<GridColumn Field="@col.Key" Width="120px" Visible="@col.Value" />
}
</GridColumns>
</TelerikGrid>
<TelerikWindow Modal="true" @bind-Visible="@isModalVisible">
<WindowTitle>
<strong>Column Chooser</strong>
</WindowTitle>
<WindowContent>
@foreach (var col in Columns)
{
var id = $"{col.Key}Column";
<TelerikCheckBox Value="@col.Value" ValueChanged="@((bool value) => ValueChanged(col.Key, value))" Id="@id"></TelerikCheckBox>
<label for="@id">@col.Key Column</label>
}
</WindowContent>
<WindowActions>
<WindowAction Name="Minimize" />
<WindowAction Name="Maximize" />
<WindowAction Name="Close" />
</WindowActions>
</TelerikWindow>
@code {
publicbool isModalVisible { get; set; }
public Dictionary<string, bool> Columns { get; set; } = new Dictionary<string, bool>
{
{ nameof(SampleData.Id), true },
{ nameof(SampleData.Name), true },
{ nameof(SampleData.Team), true },
{ nameof(SampleData.HireDate), true }
};
privatevoidValueChanged(string key, boolvalue)
{
Columns[key] = value;
}
publicclassSampleData
{
publicint Id { get; set; }
publicstring Name { get; set; }
publicstring Team { get; set; }
public DateTime HireDate { get; set; }
}
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData
{
Id = x,
Name = "name " + x,
Team = "team " + x % 5,
HireDate = DateTime.Now.AddDays(-x).Date
});
}
Regards,
Svetoslav Dimitrov
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
@code{
foreach(var col in GHL.GridColumns){
col.Title...
... will not work, because "GridColumns" is RenderFragment.
}
1) - Any idea how to iterate DECLARED columns in grid? (GridState is not enought, there is missing "Title" for end user) 2)- Any alternative how to show colum chooser by code(or somehow)? (Imagine scenario when NO columns are initialy visible in grid to the end user)
Thank you
Marin Bratanov
Telerik team
commented on 28 Mar 2022, 04:40 PM
In Blazor, the typical approach is to define the data in the view-model, and build the component based on that. So, some descriptor class for the columns can be your data source and a foreach loop over it can create your columns, while filtering it can let you loop over them. You can find a basic example at the bottom of this thread: https://feedback.telerik.com/blazor/1450105-column-chooser