aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Blazor QuickGrid - Add class based on row item context property value

Open aditya119 opened this issue 5 months ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I need to add class to a row cell based on its value. Something like:

@inject DataSource Data

<div class="grid">
    <QuickGrid Items="@Data.People">
        <PropertyColumn Property="@(c => c.PersonId)" Sortable="true"  />
        <PropertyColumn Property="@(c => c.FirstName)" Sortable="true" />
        <PropertyColumn Property="@(c => c.LastName)" Sortable="true" />

        <PropertyColumn Property="@(c => c.BirthDate)" Format="yyyy-MM-dd" Sortable="true" Class="@GetClass(context.BirthDate)" />
    </QuickGrid>
</div>

@code {
    string GetClass(DateOnly birthDate) => birthDate.Year < 2000 ? "table-success" : "table-warning"; 
}

The item context property is not accessible within Class property of PropertyColumn.

I tried to use TemplateColumn, but it only works for the tag within the cell and not the entire cell itself.

<TemplateColumn>
    <div class="@GetClass(context.BirthDate)">
        @context.ToString("yyyy-MM-dd")
    </div>
</TemplateColumn>

This also means that I have to manually handle sorting for the column which was automatically available through PropertyColumn tag.

Describe the solution you'd like

There should be a CellClassFunc property in PropertyColumn which accepts a function with ItemModel as a parameter and string as return value. The row item context should be accessible for that property.

Additional context

The question was also raised on StackOverflow

aditya119 avatar Feb 13 '24 08:02 aditya119

It seems that this feature won't be available for some time. Is there some workaround on how the requested functionality could be achieved in current version of QuickGrid? I was trying to extend PropertyColumn class to append a dynamic class string to the Class property but could not figure out how to do so.

aditya119 avatar Feb 23 '24 10:02 aditya119