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

Subclass PropertyColumn for QuickGrid display name support #32628

Closed
guardrex opened this issue May 22, 2024 · 1 comment · Fixed by #32911
Closed

Subclass PropertyColumn for QuickGrid display name support #32628

guardrex opened this issue May 22, 2024 · 1 comment · Fixed by #32911

Comments

@guardrex
Copy link
Collaborator

guardrex commented May 22, 2024

Description

Add a section on this until the PU addresses it on dotnet/aspnetcore#49147.

The code will be something like (either of the following from devs in discussion on the PU issues will work) ...

public class DisplayNamePropertyColumn<TGridItem, TProp> : PropertyColumn<TGridItem, TProp>
{
    protected override void OnParametersSet()
    {
        if (Title is null && Property.Body is MemberExpression memberExpression)
        {
            var memberInfo = memberExpression.Member;
            var displayName = memberInfo?.GetCustomAttribute(typeof(DisplayNameAttribute)) as DisplayNameAttribute;
            var display = memberInfo?.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute;
            Title = displayName?.DisplayName ?? display?.Name ?? memberInfo?.Name ?? "";
        }

        base.OnParametersSet();
    }
}

... or ...

public class DisplayNamePropertyColumn<TGridItem, TProp> : PropertyColumn<TGridItem, TProp>
{
    protected override void OnParametersSet()
    {
        if (Title is null && Property.Body is MemberExpression memberExpression)
        {
            var memberInfo = memberExpression.Member;
            Title = memberInfo?.GetCustomAttributes<DisplayNameAttribute>(false).FirstOrDefault()?.DisplayName ??
                    memberInfo?.GetCustomAttributes<DisplayAttribute>(false).FirstOrDefault()?.Name ??
                    memberInfo?.GetCustomAttributes<DisplayNameAttribute>(true).FirstOrDefault()?.DisplayName ??
                    memberInfo?.GetCustomAttributes<DisplayAttribute>(true).FirstOrDefault()?.Name ??
                    memberInfo?.Name;
        }

        base.OnParametersSet();
    }
}

... with the model DA as ...

[Display(Name = "Release Date")]
public DateTime ReleaseDate { get; set; }

... and the subclassed column in the QG ...

<DisplayNamePropertyColumn Property="movie => movie.ReleaseDate" />

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/components/quickgrid?view=aspnetcore-8.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/components/quickgrid.md

Document ID

564e71e3-82ed-ce23-4f9f-a00a58734a62

Article author

@guardrex

@guardrex
Copy link
Collaborator Author

We'll be going with ... 🥁🥁🥁 PLZ ...

if (Title is null && Property.Body is MemberExpression memberExpression)
{
    var memberInfo = memberExpression.Member;
    Title =
        memberInfo.GetCustomAttribute<DisplayNameAttribute>().DisplayName ??
        memberInfo.GetCustomAttribute<DisplayAttribute>().Name ??
        memberInfo.Name;
}

base.OnParametersSet();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants