How to set Grid Grouping Fields based on value in model

1 Answer 143 Views
Grid
Daniel
Top achievements
Rank 1
Daniel asked on 08 Aug 2023, 08:10 AM

Hi

Currently, I'm using the following code to display data with initial grouping:

...
.DataSource(dataSource => dataSource                 .Ajax()                 .Read(read => read.Action("Method""Controller"))                 .ServerOperation(false)                 .GroupPaging(true)                 .PageSize(50)                 .Group(g =>                      {                         g.Add(x => x.JahrText);                         g.Add(x => x.QuartalText);                         g.Add(x => x.MonatText);                         g.Add(x => x.TagText);                     }                 )                 .Aggregates(aggr =>                 {                     aggr.Add(e => e.JahrText).Count();                     aggr.Add(e => e.QuartalText).Count();                     aggr.Add(e => e.MonatText).Count();                     aggr.Add(e => e.TagText).Count();                     aggr.Add(e => e.Description).Count();                 })

...

Now I'd like to do the initial grouping (when the page gets rendered for the first time) based on a value in the model.
Let's say, if the model property "GroupLevel" is set to 1, only the "JahrText" is added to the group.
If the property "GroupLevel" is set to 2, "JahrText" and "QuartalText" are added to the group, etc.

Is there an easy way to implement such a behaviour?

Thank you for your support!

Best regards,

Daniel

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 10 Aug 2023, 12:12 PM

Hello Daniel,

 

Thank you for writing to us.

You can achieve this requirement using the following approach:

1. Prepare a condition (e.g. IsUserRole bool value):

@{
    bool condition = 2 + 2 == 5;
}
2. Use it in the Grid's Group definitions:
                .Group(g =>
                {
                    g.Add(x => x.ShipName);
                    if (condition)
                    {
                        g.Add(x => x.ShipCity);
                    }
                }
This works properly on my side. Feel free to check it and let me know if it helps you. Do not hesitate to let me know if I made a mistake somewhere or you need more particular and specific example/code. I will be ready to help you with any further inquires you might have.

 

Regards,
Eyup
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.

Daniel
Top achievements
Rank 1
commented on 14 Aug 2023, 04:46 PM

Hi Eyup

Thank you very much for your help. Your proposed solution works like a charm!

Best regards,

Daniel

Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or