How to set aggregate using WebApiAdaptor

Dear syncfusion,

i am use ejgrid control using  WebApiAdaptor. i have problem aggregate  function apply in ODataQueryOptions (System.Web.Http.OData) api method.

js code is below

dataSource(ej.DataManager({
                url: ServiceURI + "/api/Reportgrid,
                adaptor: new ej.WebApiAdaptor(),
                crossDomain: true
            }));


my api code is below.

{
                listab = (List<TESTCLASS>)reply;
            }
            var temp = listab.AsQueryable();
            try
            {
                Count = listab.Count();

                if (opts.OrderBy != null)
                    temp = opts.OrderBy.ApplyTo(temp);

                temp = opts.ApplyTo(temp, new ODataQuerySettings()).Cast<TESTCLASS>();

                //if (opts.InlineCount != null)
                //{
                //    Count = _obj.Count();
                //}
                if (opts.RawValues != null)
                {
                    int _SKIPVAL = Convert.ToInt32(opts.RawValues.Skip);
                    int _TOPVAL = Convert.ToInt32(opts.RawValues.Top);
                    temp = temp.Skip(_SKIPVAL).Take(_TOPVAL);
                }



3 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team August 23, 2018 09:59 AM UTC

Hi Viral,  
 
Thanks for contacting Syncfusion Support.  
 
By default, Aggregates/Summary for the WebApiAdaptor cannot be displayed. Since the summary cannot be calculated for the whole record count in the remote data binding, Aggregate Query will not be generated. This is the default behavior of the Grid. However, we can display the custom summary using the model.currentViewData of the Grid. Refer to the following code example.  
 
<div id="Grid"></div> 
 
<script type="text/javascript"> 
    $(function () { 
        $("#Grid").ejGrid({ 
                . . 
                 . . . 
            summaryRows: [ 
                { title: "Currency", 
                    summaryColumns: [ 
                        { summaryType: ej.Grid.SummaryType.Custom,  
                        customSummaryValue: currency,  
                        displayColumn: "Freight", format: "{0:C2}" } 
                    ] 
                } 
                  . . . 
            ], 
        }); 
 
    }); 
 
    function currency() { 
        var gridObj = $("#Grid").ejGrid("instance") 
        var rs=ej.sum(gridObj.model.currentViewData, "Freight") 
        var dol = 0.017 
        return (rs * dol); 
    } 
 
</script> 
 
Regards,  
Seeni Sakthi Kumar S. 



VI Viral August 24, 2018 04:15 AM UTC

But i am using paging in server side  and i need all record count how it's work?


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team August 24, 2018 11:19 AM UTC

Hi Customer,  
 
Usually, remote data handlers like WebAPIAdaptor, OdataAdaptor, return only the current page records which is the default behavior. So, we can summarize the current page records only in the Grid Summary Footer. The concept behind these adaptors is bring the load on demand which means client-end receives only the current page records. Based on the received records only these adaptors summarize the aggregates and displays them in Grid. If we return whole records to the client-end, concept of the load on demand will be declined. Henceforth, based on the current page records the summary were displayed.  
 
However, we have achieved your requirement using the custom adaptors and custom headers of the DataManager. Using the custom headers of the required aggregate fields were shared to the server-end. Based on that selective fields with their records were return to the client as aggregates along with the Items/count. Refer to the following code example.  
 
Client side: 
 
<script type="text/javascript"> 
    $(function () { 
        var customadaptor = new ej.WebApiAdaptor().extend({ 
            processResponse: function (data, ds, query, xhr, request, changes) { 
                var resultdata = data, pvt = request && request.ejPvtData;; 
                var result = this.base.processResponse.apply(this, [data, ds, query, xhr, request, changes]); 
                if (pvt && pvt.aggregates && pvt.aggregates.length) { 
                    var agg = pvt.aggregates, args = {}, fn, res = {}; 
                    for (var i = 0; i < agg.length; i++) { 
                        fn = ej.aggregates[agg[i].type]; 
                        if (fn) 
                            res[agg[i].field + " - " + agg[i].type] = fn(resultdata.aggregate, agg[i].field); 
                    } 
                    result.aggregates = res; 
               } 
                return result; 
            } 
        }); 
 
            $("#Grid").ejGrid({ 
                dataSource: ej.DataManager({ 
                    url: "/api/Values", 
                    adaptor: new customadaptor() 
                }), 
                   . . . 
                       . . . 
                allowPaging: true, 
                showSummary: true, 
                actionBegin: function(args){ 
                    var agg = this.model.query.queries.filter(function (e) { return e.fn === "onAggregates" }); 
                    var aggregate = []; 
                    for (var s = 0; s < agg.length; s++) { aggregate.push(agg[s].e.field); }; 
                    this.model.dataSource.dataSource.headers = []; 
                    this.model.dataSource.dataSource.headers.push({ aggregates: JSON.stringify(aggregate) }); 
                }, 
           }); 
        }); 
</script> 
 
Server Side: 
 
public object Get() 
        { 
            if (order.Count == 0) 
                bindData(); 
            List<Orders> data = order; 
            DataOperations ds = new DataOperations(); 
            string agg = Request.Headers.GetValues("aggregates").ToList()[0]; 
            List<string> aggregates = (List<string>)JsonConvert.DeserializeObject(agg, typeof(List<string>)); 
            List<string> str = new List<string>(); 
            IEnumerable aggregate = ds.PerformSelect(order, aggregates); 
           return new { Items = data.Skip(skip).Take(take), Count = order.Count(), aggregate = aggregate }; 
        } 
 
 
Note: Increase in the number of summary columns, may increase the overload for the results returned from the server. 
 
Sample can be downloaded from the following location.  
 
 
Refer to the following KB for custom headers of the DataManager, https://www.syncfusion.com/kb/5963/ 
 
Regards,  
Seeni Sakthi Kumar S. 


Loader.
Up arrow icon