Completed
Last Updated: 13 Dec 2021 07:57 by Ironoak
Release 2.9.0
Michael
Created on: 04 Oct 2019 06:01
Category: Grid
Type: Feature Request
26
Set Grouping from code
At this point only the user can group the grid. I want to be able to set initial grouping with my code. This should also allow me to group by columns that are not visible/rendered at the moment.
10 comments
Ironoak
Posted on: 13 Dec 2021 07:57

Hi Marin,

thanks for the quick response.

That works well indeed. My error was that I forgot to include the grouped column in the grid definition.

 

Regards,

Ewald

ADMIN
Marin Bratanov
Posted on: 11 Dec 2021 09:03

Hi Ewald,

Have you had a chance to try the OnStateInit event: https://docs.telerik.com/blazor-ui/components/grid/state#set-default-initial-state?

Here's a sample I made for you that seems to work fine: https://blazorrepl.telerik.com/QPlmlvOZ02KK6pio32. If this does not help you move forward, I recommend opening a support ticket where you can share more details and snippet so we can look into the situation.

Regards,
Marin Bratanov
Progress Telerik

Learn about the important changes coming in UI for Blazor 3.0 in January 2022!
Ironoak
Posted on: 10 Dec 2021 11:34

None of the methods in the examples seem to work when the data for the grid are fetched asynchronously.

Please provide an example where data is fetched in the OnInitializedAsync() method.

 

Regards,

Ewald

ADMIN
Marin Bratanov
Posted on: 05 May 2020 07:49

Hi Tom,

The group indicator does not have, and should not have access to the data. It should show which field/column is used to group by. THere may be many values to group by and they can't all be shown in the indicator. They are shown in the group header, and for that - there is a template: https://docs.telerik.com/blazor-ui/components/grid/templates#group-header

Please open a ticket if you can't get around the available documentation so I can help you according to the case, and so we don't spam everyone else who monitored the grouping from code feature.

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tom
Posted on: 04 May 2020 21:56

Hi Marin,

We're getting closer. The grouping using the OnStateInit method now works as desired. It would be nice if you added the code to the example shown in the documentation. The documentation only shows how to set the SortDirection.

However, your example of how to set the DisplayContent is insufficient. I don't want a constant string but rather the value, in my case, of the category field on which I am doing the grouping. Or, in your example, the name of the team being grouped on. There is no intelliSense displayed when setting the DisplayContent.

ADMIN
Marin Bratanov
Posted on: 04 May 2020 14:31

Hi Tom,

The OnStateInit event is what you use to do that, and using it is showcased in the documentation article.

If you want to use the .SetState() method, you must use the OnAfterRender event, because that's when component references are populated by the framework.

The DisplayContent field of the group descriptor can alter what you see in the group indicator, even though it is supposed to be the column name by default so the users can make the connection between the grouping and the column/field. Its intellisense comment provides instructions on setting a string.

I made an example for this (attached below), and if you have further questions I'd ask that you open a support ticket or a forum thread so we can keep the portal clean - it is a place for tracking and discussing new features and bugs, and not for general purpose technical support, and we won't spam everyone who has been following this feature.

 

@using Telerik.DataSource;

<TelerikGrid Data="@MyData" Height="400px" Groupable="true" Navigable="true"
             Pageable="true" FilterMode="@GridFilterMode.FilterMenu"
              OnStateInit="@((GridStateEventArgs<SampleData> args) => OnStateInitHandler(args))">
    <GridColumns>
        <GridColumn Field="@(nameof(SampleData.Id))" Width="120px" Groupable="false" />
        <GridColumn Field="@(nameof(SampleData.Name))" Title="Employee Name" />
        <GridColumn Field="@(nameof(SampleData.Team))" Title="Team" />
        <GridColumn Field="@nameof(SampleData.IsOnLeave)" Title="On Vacation" />
        <GridColumn Field="@(nameof(SampleData.HireDate))" Title="Hire Date" />
    </GridColumns>
</TelerikGrid>

@code {
    async Task OnStateInitHandler(GridStateEventArgs<SampleData> args)
    {
        GridState<SampleData> desiredState = new GridState<SampleData>()
        {
            GroupDescriptors = new List<GroupDescriptor>()
            {
                new GroupDescriptor()
                {
                    Member = "Team",
                    DisplayContent = "TEAM GROUP INDICATOR", // this is how to set the string for the group indicator
                    MemberType = typeof(string)
                },
                new GroupDescriptor()
                {
                    Member = "IsOnLeave",
                    MemberType = typeof(bool),
                    SortDirection = ListSortDirection.Descending // not required, but a feature not yet available through the UI
                }
            }
        };

        // this is how to pass the state object you generate to the grid in the OnStateInit event
        args.GridState = desiredState;
    }

    public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x => new SampleData
    {
        Id = x,
        Name = "name " + x,
        Team = "team " + x % 5,
        IsOnLeave = x % 2 == 0,
        HireDate = DateTime.Now.AddDays(-x).Date
    });

    public class SampleData
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Team { get; set; }
        public bool IsOnLeave { get; set; }
        public DateTime HireDate { get; set; }
    }
}

 

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tom
Posted on: 01 May 2020 20:37

Neither of your references describe how to show the grid with the grouping already applied. Clicking on a button after the grid appears works fine, but that's not what I want to do. Calling the SetGridGroup method (modified for my code) in OnInitializedAsync causes an exception: "The render handle is not yet assigned". So the question is: Where should the SetGridGroup method be called?

Also, how can you change the default display of the group title? Currently, in my case, the title shows "category: Medical". category is the name of the field being grouped on. I just want the title to Medical  in this case. There is a DisplayContent property in the GroupDescriptor which seems like that is where I would make the change I desire but I haven't been able to figure out what the syntax is. Please provide some examples of the use of this field.

ADMIN
Marin Bratanov
Posted on: 27 Apr 2020 07:30

Hello Tom,

There are several examples you can use as base:

The entire article about the grid state offers more examples and explanations on what the state contains and does, so you might find it useful in its entirety.

 

Regards,
Marin Bratanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tom
Posted on: 26 Apr 2020 22:18
This says it was completed in Release 2.9.0. I'm running 2.11.0. Do you have example code as to how to use this feature, i.e., to group programmatically on a not-visible column at initialization?
ADMIN
Marin Bratanov
Posted on: 24 Oct 2019 06:13

Perhaps it should allow defining default collapsed/expanded state for the given group field as well, see here.

 

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor