Grid sort by group aggregate

1 Answer 270 Views
Grid
Boris
Top achievements
Rank 1
Iron
Iron
Boris asked on 27 Apr 2022, 05:00 PM

Hi there,

I am having trouble configuring the Grid sort by group aggregate. I was following the tutorial( https://docs.telerik.com/kendo-ui/knowledge-base/grid-sort-group-by-group-aggregate ) that was done in jQuery, but I didn't manage to configure the grid properly. Can someone share the same configuration in .NET Core?

Thanks.

1 Answer, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 02 May 2022, 09:34 AM

Hello Boris,

Defining a custom compare handler works only for client side operations and cannot be achieved at this point using the server wrapper.

However, you can bind the .Net Core grid to a JS defined dataSource with a custom compare handler as follows:

@(Html.Kendo().Grid<...>()
    .Name("Grid")
...
    .DataSource("dataSource")
)
<script>
 var dataSource = new kendo.data.DataSource({
        ...
        group: {
          field: "customer",
          aggregates: [
            { field: "july", aggregate: "sum" },
            { field: "aug", aggregate: "sum" },
            { field: "sep", aggregate: "sum" },
            { field: "total", aggregate: "sum" }
          ],
          dir: "asc",
          compare: function(a, b) {
            var a = a.items.map(x=>x.total);
            var b = b.items.map(x=>x.total);
            var reducer = (accumulator, currentValue) => accumulator  + currentValue;
            var aTotalSum = a.reduce(reducer);
            var bTotalSum = b.reduce(reducer);
            if (aTotalSum == bTotalSum) {             
              return 0;
            } else if (aTotalSum > bTotalSum) {
              return 1;
            } else {
              return -1;
            }
          }
        },
        sort: { 
          field: "total", 
          dir: "asc"
        }
      });
</script>
 I hope this helps.

Regards,
Georgi
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Boris
Top achievements
Rank 1
Iron
Iron
Answers by
Georgi
Telerik team
Share this question
or