I have a requirement where one of the column value is a html string and I need to convert that value as markup string. I can do it using a template as:
<GridColumn Field="@nameof(Employee.Name)" Title="Name">
<Template>
@{
var value = (context as Employee);
var markupValue = (MarkupString) value.Name;
@markupValue;
}
</Template>
</GridColumn>
But I want to create a dynamic component (suitable for any class and any property). I created a custom component and called it from the main component. The requirement was achieved but I lost the sorting functionality and also this grid column is moved towards the end.
Custom component (HtmlGrid.razor):
<GridColumn Title="@Title">
<Template>
@{
var value = GetPropValue(context, ColumnName)?.ToString();
var a = (MarkupString)value;
@a;
}
</Template>
</GridColumn>
@code {
[Parameter] public string ColumnName { get; set; } = string.Empty;
[Parameter] public string Title { get; set; } = string.Empty;
public object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}
}
This is then called from some parent (Employee.razor) as:
<GridColumn Field="@(nameof(Employee.Id))" Width="80px" />
<GridColumn Field="@(nameof(Employee.A))" Width="80px" />
<HtmlGrid ColumnName="@nameof(Employee.Name)" Title="Name"/>
<GridColumn Field="@(nameof(Employee.B))" Width="80px" />
The issue here is that, when this grid is rendered, the columns are seen in the order:
ID-Column, A-Column, B-Column, Name-Column (HtmlGrid)
Also, There is no sorting in this HtmlGrid