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

feat: add [Parameter] to/in DataGrid TemplateColumn SortBy #2041

Closed
kamazheng opened this issue May 13, 2024 · 7 comments
Closed

feat: add [Parameter] to/in DataGrid TemplateColumn SortBy #2041

kamazheng opened this issue May 13, 2024 · 7 comments
Labels
closed:not-actionable There is no action to be taken in response to this issue.

Comments

@kamazheng
Copy link

kamazheng commented May 13, 2024

Is it possible to update DataGrid TemplateColumn SortBy to accept some parameters?
For some dynamic situations,

            @foreach (var key in DicKeys)
            {
                <TemplateColumn Title="@property.DisplayName" Align="@Align.Center" Style="width:100%"
                        Tooltip=true SortBy="@(() => sortBy(key))">
                    <ChildContent>
                        ......
                    </ChildContent>
                </TemplateColumn>
            }          
@microsoft-github-policy-service microsoft-github-policy-service bot added the triage New issue. Needs to be looked at label May 13, 2024
@vnbaaij vnbaaij added needs: author feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed triage New issue. Needs to be looked at labels May 14, 2024
@vnbaaij
Copy link
Collaborator

vnbaaij commented May 14, 2024

You can already do that, just not directly in the parameter (example comes from the demosite):

@inject DataSource Data

<FluentDataGrid Items="@Data.People" style="width: 600px;">
    <TemplateColumn Title="Person" SortBy="@sortByName">
        <div class="flex items-center">
            <img class="flag" src="_content/FluentUI.Demo.Shared/flags/@(context.CountryCode.ToLower()).svg" alt="Flag of @(context.CountryCode)" />
            <nobr>
                <strong>@context.LastName</strong>, @context.FirstName
            </nobr>
        </div>
    </TemplateColumn>
    <TemplateColumn Title="Bonus">
        <FluentButton Appearance="Appearance.Accent" @onclick="@(() => Bonus(context))">Regular</FluentButton>
        <FluentButton Appearance="Appearance.Accent" @onclick="@(() => DoubleBonus(context))">Double</FluentButton>
    </TemplateColumn>
</FluentDataGrid>


<p><strong>@message</strong></p>

@code {
    string message = string.Empty;

    GridSort<Person> sortByName = GridSort<Person>
        .ByAscending(p => p.LastName)
        .ThenAscending(p => p.FirstName);

    void Bonus(Person p) => message = $"You want to give {p.FirstName} {p.LastName} a regular bonus";

    void DoubleBonus(Person p) => message = $"You want to give {p.FirstName} {p.LastName} a double bonus";
}

What are you missing?

@kamazheng
Copy link
Author

My situation is the TemplateColumn created by foreach loop, it's dynamically created. Each TemplateColumn has different parameter for sorting. For example, we have dynamic object, the properties is not known until runtime, I need to pass each property to specific TemplateColumn for sorting, same as to have DataGrid context of List of key-value Dictionary.

@vnbaaij
Copy link
Collaborator

vnbaaij commented May 15, 2024

I think you can do that with a method that returns a GridSort result. You kan use the @key as a parameter for that method.

@kamazheng
Copy link
Author

I think you can do that with a method that returns a GridSort result. You kan use the @key as a parameter for that method.

The TemplateColumn has no onClick event before sort, cannot pass the current @key to the method which return GridSort.

@vnbaaij
Copy link
Collaborator

vnbaaij commented May 15, 2024

The @key comes from the foreach loop in your example. Passing that to a method does exactly the same as using the inline variant that you are suggesting

@kamazheng
Copy link
Author

The @key comes from the foreach loop in your example. Passing that to a method does exactly the same as using the inline variant that you are suggesting

Thanks. It works.
I have another question, when I add sortBy, the header row will add button, how to change the background-color of the button?
The XPath:
//*[@id="fdb48c27"]/fluent-data-grid/fluent-data-grid-row[1]/fluent-data-grid-cell[2]/fluent-button//button

The button is a shadow element, cannot easy to add css style for it.

image

@vnbaaij
Copy link
Collaborator

vnbaaij commented May 16, 2024

This is possible but goes directly against the purpose of this library: supplying components leveraging the Fluent UI design system.

You can target the component with CSS by using the ::deep selector and then specifying which part of the shadow DOM you want to target by using the standard ::partselector. Something like:

::deep > fluent-button::part(control){
...your css...
} 

Closing this as the original issue is not an issue and provided guidance works.

@vnbaaij vnbaaij added closed:not-actionable There is no action to be taken in response to this issue. and removed needs: author feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels May 16, 2024
@vnbaaij vnbaaij closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed:not-actionable There is no action to be taken in response to this issue.
Projects
None yet
Development

No branches or pull requests

2 participants