Filter out functions from Aggregates context menu

1 Answer 67 Views
Grid
Harshad
Top achievements
Rank 1
Harshad asked on 04 Jul 2023, 08:40 AM | edited on 13 Jul 2023, 06:11 AM

I am using Telerik RadGrid control in my ASP.NET web application. Grid columns are generated automatically from the data source. Also Grouping, sorting, Filtering, Aggregates etc are enabled for the columns in the grid. On right click on the column, it shows context menu and after selecting "Aggregates" option it shows all functions available such as  None,Sum,Min,Max,Last,First,Count,Avg,CountDistinct,Custom

But I would like to remove some functions from this list.

 

Is there a way to access the functions list and manipulate it before grid renders ?

 

Thanks

Harshad

1 Answer, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 07 Jul 2023, 07:32 AM

Hi Harshad,

The Aggregates menu by default shows the applicable aggregates for the current column. The Menu, on the other hand, is just one object that has its items populated based on which specific column it is open for.

Therefore it seems most convenient to use the client-side OnHeaderMenuShowing event to manipulate the menu items separately for each column just before the menu is displayed.

Here is one example:

<ClientSettings>
    <ClientEvents OnHeaderMenuShowing="headerMenuShowing" />
</ClientSettings>

JavaScript

function headerMenuShowing(sender, args) {
    var column = args.get_gridColumn()
    //apply the customization only for desired column by its unique name
    if (column.get_uniqueName() == "Freight") {
        var menu = args.get_menu();
        var aggregatesItem = menu.findItemByText("Aggregates");
        var aggregatesMenuItems = aggregatesItem.get_items();
        for (var i = 0; i < aggregatesMenuItems.get_count(); i++) {
            //search for the desired aggregates option and hide the item
            if (aggregatesMenuItems.getItem(i).get_text() == 'Sum') {
                aggregatesMenuItems.getItem(i).hide();
            }
        }
    } 
}

Please give this a try and let me know how it goes.

Kind regards,
Doncho
Progress Telerik

Heads up! Telerik UI for ASP.NET AJAX versions for .NET 3.5 and 4.0 are retired. Progress will continue shipping assemblies compatible with .NET 4.5 and later. See whether this affects your apps in this article.
Harshad
Top achievements
Rank 1
commented on 10 Jul 2023, 10:07 AM | edited

Hi Doncho

Thank you for your answer.

It works as you suggested expected. I found another way to achieve same result but instead of Javascript, I am handling it in the code-behind in GridHeaderContextMenu PreRender event handler

I want to hide / remove some functions globally no matter the grid columns supports it or not

This is rough version of how I did it using VB and since it works I will work on making it more flexible

Public Sub GridHeaderContextMenu_PreRender()

            Dim allowedAggregates As New List(Of String) From {
                "None",
                "Sum",
                "Avg",
                "Min",
                "Max",
                "Count"
            }

            Dim menuItem As RadMenuItem = HeaderContextMenu.FindItemByText("Aggregates")

            If (menuItem IsNot Nothing) Then

                For i As Integer = menuItem.Items.Count - 1 To 0 Step -1
                    If (Not allowedAggregates.Contains(menuItem.Items(i).Text)) Then
                        menuItem.Items.RemoveAt(i)
                    End If
                Next

            End If

        End Sub

 

Let me know if you think this could cause issues.

 

Thanks,

Harshad

Attila Antal
Telerik team
commented on 13 Jul 2023, 06:14 AM

Hi Harshad,

I don't see why it could cause issues, but if it does, let us know. Describe the issue and share the full implementation with us to investigate it.

Tags
Grid
Asked by
Harshad
Top achievements
Rank 1
Answers by
Doncho
Telerik team
Share this question
or