Sub-Total Row

10 posts, 1 answers
  1. Lee
    Lee avatar
    61 posts
    Member since:
    Feb 2018

    Posted 02 Mar 2018 Link to this post

    Hi,

    I'm using the currently using the Grid with server side processing, grouped by Project Name, and using the ClientGroupFooterTemplate to display totals. This works great, but is there a way to add a sub-total row, for some other calculations?

    I'm currently querying the items needed and then query the sub-totals row then concatenating the queries this is drastically slowing the load time.

    Any ideas?

    public ActionResult ProjectTaskListAjax(Guid id, [DataSourceRequest] DataSourceRequest request)
    {
        var tasks = DataService.GetQueryableProjectTasksByUploadId(id);
     
        var queries = new[]
        {
            // This gets all the current tasks
            tasks.Select(x => new GridProjectTaskModel
            {
                [...]
                LabourCostTotal = x.LabourCostTotal,
                MaterialCostTotal = x.MaterialCostTotal,
                [...]
            }),
     
            // This gets all the tasks then groups by the Project and then calculates two columns that need to be displayed on the 'sub-total' row.
            tasks.GroupBy(x => x.Project.Name).Select(g => new GridProjectTaskModel
            {
                [...]
                LabourCostTotal = g.FirstOrDefault().PurchaseOrders.Where(x => x.SupplierType == "Sub-Contract Labour").Sum(x => x.TotalEx),
                MaterialCostTotal = (g.FirstOrDefault().PurchaseOrders.Where(x => x.SupplierType == "Sub-Contract Labour").Sum(x => x.TotalEx) * -1),
                [...]
            })
        };
     
        // Concat vs Union makes no difference here
        var mergedQuery = queries.Select(query => query.AsEnumerable()).Aggregate(Enumerable.Union);
     
        return Json(mergedQuery.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
    }
  2. Answer
    Viktor Tachev
    Admin
    Viktor Tachev avatar
    2501 posts

    Posted 06 Mar 2018 Link to this post

    Hi Lee,

    If you would like to display multiple aggregates for a given column you can use an approach similar to the one illustrated in the example below:


    With that said, based on the provided description it seems that the slow performance is due to the complexity of the query. Have in mind that this is not directly related to the Grid component. Nevertheless, I can suggest checking out the article below that discusses optimization techniques for Entity Framework queries.


    Regards,
    Viktor Tachev
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Neil
    Neil avatar
    38 posts
    Member since:
    Dec 2019

    Posted 28 Apr 2020 in reply to Viktor Tachev Link to this post

    Does the Aggregate functions nee to have a group
     
    .Group(groups => groups.Add(p => p.UnitsInStock))

    Because I can't get the sum.. I just want to get the Overall Sum, i.e. Qty of items in the grid.

     

  4. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2501 posts

    Posted 29 Apr 2020 Link to this post

    Hello Neil,

     

    Out of the box aggregates will be displayed for the entire Grid and for each group. If data in the Grid is not grouped only the total aggregates will be shown.

     

    Regards,
    Viktor Tachev
    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.
  5. Neil
    Neil avatar
    38 posts
    Member since:
    Dec 2019

    Posted 30 Apr 2020 in reply to Viktor Tachev Link to this post

    I have something like this, but no suma is displayed..

     

    col.Bound(c => c.Qty).Width(100)
                        .FooterTemplate(@<text>Total: @item.Sum</text>);

  6. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2501 posts

    Posted 04 May 2020 Link to this post

    Hello Neil,

     

    The FooterTemplate option will be rendered on the server. Use ClientFooterTemplate instead and see how the behavior changes:

    col.Bound(c => c.Qty).Width(100)
                        .ClientFooterTemplate("Total: #= sum #");

     

    Regards,
    Viktor Tachev
    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.
  7. Neil
    Neil avatar
    38 posts
    Member since:
    Dec 2019

    Posted 04 May 2020 in reply to Viktor Tachev Link to this post

    I got "Uncaught Reference Error: sum is not defined", when using .ClientFooterTemplate
    .

    With my issue, i only got a blank data here. 

    Take a look at my grid, and see what did I miss..

  8. Preslav
    Admin
    Preslav avatar
    591 posts

    Posted 06 May 2020 Link to this post

    Hello Neil,

    As I cannot see the full code of the Grid, I am not sure what is causing this issue.

    I assume that the Aggregates calculation in the DataSource is missing. For example, in this demo - https://demos.telerik.com/aspnet-mvc/grid/aggregates the Sum of the UnitPrice is calculated like this:

        .DataSource(dataSource => dataSource
            .Ajax()
            .Aggregates(aggregates =>
            {
                aggregates.Add(p => p.UnitsInStock).Min().Max().Count();
                aggregates.Add(p => p.UnitsOnOrder).Average();
                aggregates.Add(p => p.ProductName).Count();
                aggregates.Add(p => p.UnitPrice).Sum();
            })
            .Group(groups => groups.Add(p => p.UnitsInStock))
            .Read(read => read.Action("Aggregates_Read", "Grid"))
        )

    Please, check if adding the above resolves the problem.

     

    Regards,
    Preslav
    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.
  9. Neil
    Neil avatar
    38 posts
    Member since:
    Dec 2019

    Posted 06 May 2020 in reply to Preslav Link to this post

    Here's the snippet of the grid I've been having....

    @(Html.Kendo().Grid<TelerikMvcApp2.Models.vw_Detail>()
            .Name("grid")
            .AutoBind(false)
            .Columns(col =>
            {
            col.Bound(c => c.SNo);
            col.Bound(c => c.ItemCode);
            col.Bound(c => c.Description);
            col.Bound(c => c.Qty).Width(100).HtmlAttributes(new { style = "text-align:right;" })
                  .FooterTemplate(@<text> Total: @item.Sum </text>)
    ;
    col.Bound(c => c.Unit);
    })
    .Pageable()
    .DataSource(ds =>
    ds.Ajax()
    .Aggregates(ag =>
    {
    ag.Add(p => p.Qty).Sum();
    })
    .PageSize(5)
    .Read(a => a.Action("Read_DetailGrid", "DASH"))

    )
    )

     


  10. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2501 posts

    Posted 08 May 2020 Link to this post

    Hello Neil,

     

    I suggest using ClientFooterTemplate to display the aggregate value. Like this:

     

    @(Html.Kendo().Grid<TelerikMvcApp2.Models.vw_Detail>()
    	.Name("grid")
    	.AutoBind(false)
    	.Columns(col =>
    	{
    		col.Bound(c => c.SNo);
    		col.Bound(c => c.ItemCode);
    		col.Bound(c => c.Description);
    		col.Bound(c => c.Qty).Width(100).HtmlAttributes(new { style = "text-align:right;" })
    			  .ClientFooterTemplate("Total: #= sum #");
    		col.Bound(c => c.Unit);
    	})
    	.Pageable()
    	.DataSource(ds =>
    		ds.Ajax()
    		.Aggregates(ag =>
    		{
    			ag.Add(p => p.Qty).Sum();
    		})
    		.PageSize(5)
    		.Read(a => a.Action("Read_DetailGrid", "DASH"))
    	)
    )

     

    In case the behavior persists please send us a runnable project where it is replicated so we can examine it.

     

    Regards,
    Viktor Tachev
    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.
Back to Top