Before I start, I have read several other questions on this forum and, although they partly answer this question, I feel I need some further clarification.
I have quite a complicated RadzenDataGrid that display properties from a list of objects (not really relevant to this).
I've had a user request that they can re-order, resize and hide columns and save it to their profile. Reading other threads about this, it seems that I cannot just re-order existing columns by changing their index, they have to be declared programmatically at the start and then the order can be changed as the order they are created can be configured.
So code similar to:
Now most the columns I have have custom filtering applied and therefore have and sections defined under the column. These often have other logic (such as if statements and sometimes calling functions) embedded in them
Short of doing a massive if statement within my for loop checking the name and then creating the appropriate and sections, is there an easy way to create these sections with my custom columns.
I.e.
foreach (var column in columnSettings.OrderBy(c => c.Index))
{
<RadzenDataGridColumn TItem="MyItem" Property=@column.Property Visible=@column.Visible>
@if (column.FilterText != null)
{
<FilterTemplate>
@column.FilterText
</FilterTemplate>
}
.... same for <Template>
</RazdenDataGridColumn>
}
Where FilterText is something similar to: <RadzenNumeric @bind-Value=idValue ShowUpDown=false Style="width:100%" InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", "filter by ID" }})" />
To be honest I am not aware of other solution - Blazor markup needs to parsed in order to render. You can use helper .razor files in order to avoid this statement becoming huge:
if (@column.Name = "MyColumn1")
{
<FilterTemplate>
<MyColumnFilterTemplate />
</FilterTemplate>
}
@Paul_Ruston - Thanks, I missed that one! The threads I had been reading did suggest that you could not change this value to re-order the columns.
But if I create a dictionary and populate it with the order, then that might work and make my life a lot easier.
Follow up for anyone who reads this later, changing the OrderIndex on component initialisation didn't work, I ended up creating all the columns in a large if statement.
To make it easier, moved each column into its own Component (except single line columns) which made if statement much more readable!