Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide option for custom transformation of column values #46

Open
gogo4ds opened this issue Sep 5, 2023 · 0 comments · May be fixed by #57
Open

Provide option for custom transformation of column values #46

gogo4ds opened this issue Sep 5, 2023 · 0 comments · May be fixed by #57
Labels
enhancement New feature or request

Comments

@gogo4ds
Copy link
Collaborator

gogo4ds commented Sep 5, 2023

The problem

The values of the properties in the grid are retrieved directly from the database models (from db) and displayed as they are.
There is no easy way to override this behavior and make transformations to the data.
For example in Judge we need to modify date fields, to convert from one time zone to another when displaying the values in the grid.
Currently we have implemented a workaround by overriding BuildGridColumns:

var gridColumns = base.BuildGridColumns(columns, stringMaxLength);

        // Convert DateTime values to local time before displaying, as the admins will be working with local time,
        // but in the database are always stored in UTC.
        // TODO: provide option to override a method that just generates the expression that gets the property value.
        // This is a workaround, but if option to override the method that gets the lambda is provided,
        // in the AutoCrudAdminOptions (GenerateColumnConfiguration), it will be much easier to just override the expression.
        gridColumns.ForEach(gc =>
        {
            var propertyInfo = typeof(TEntity).GetProperty(gc.Name);
            if (propertyInfo == null || !TypeIsDateTime(propertyInfo.PropertyType))
            {
                return;
            }

            gridColumns.Remove(gc);

            if (gc is IGridColumn<TEntity, DateTime> dateTimeGridColumn)
            {
                gridColumns
                    .Add(
                        this.GetDateTimeToLocalExpression<DateTime>(propertyInfo)
                        ?? dateTimeGridColumn.Expression)
                    .Titled(propertyInfo.Name)
                    .Filterable(true)
                    .Sortable(true);
            }
            else if (gc is IGridColumn<TEntity, DateTime?> nullableDateTimeGridColumn)
            {
                gridColumns
                    .Add(
                        this.GetDateTimeToLocalExpression<DateTime?>(propertyInfo)
                        ?? nullableDateTimeGridColumn.Expression)
                    .Titled(propertyInfo.Name)
                    .Filterable(true)
                    .Sortable(true);
            }
        });

        return gridColumns;

Proposed easy solution:

It would be much easier if we can override just the part where the lambda is generated in AutoCrudAdminController's GenerateColumnConfiguration:

var lambda = ExpressionsBuilder.ForGetProperty<TEntity, TProperty>(property);

This could become:

var lambda = GenerateColumnLambda(property);

where GenerateColumnLambda(property) is virtual method.

We would not have to remove columns and create new ones.

NOTE: with this solution, expression trees will also have to be created by the users of the library, but at least no knowledge of columns generation will be needed.

@gogo4ds gogo4ds added the enhancement New feature or request label Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant