This is a migrated thread and some comments may be shown as answers.
Sub-Total Row
9 Answers 194 Views
This is a migrated thread and some comments may be shown as answers.
Lee
Top achievements
Rank 1
Veteran
Lee asked on 02 Mar 2018, 10:05 PM

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);
}

9 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Tachev
Telerik team
answered on 06 Mar 2018, 01:21 PM
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.
0
Neil
Top achievements
Rank 1
answered on 28 Apr 2020, 07:15 AM

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.

 

0
Viktor Tachev
Telerik team
answered on 29 Apr 2020, 02:17 PM

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.
0
Neil
Top achievements
Rank 1
answered on 30 Apr 2020, 08:01 AM

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

 

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

0
Viktor Tachev
Telerik team
answered on 04 May 2020, 07:31 AM

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.
0
Neil
Top achievements
Rank 1
answered on 05 May 2020, 04:51 AM

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..

0
Preslav
Telerik team
answered on 06 May 2020, 12:29 PM

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.
0
Neil
Top achievements
Rank 1
answered on 06 May 2020, 02:13 PM

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"))

)
)

 


0
Viktor Tachev
Telerik team
answered on 08 May 2020, 09:00 AM

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.
Tags
Grid
Asked by
Lee
Top achievements
Rank 1
Veteran
Answers by
Viktor Tachev
Telerik team
Neil
Top achievements
Rank 1
Preslav
Telerik team
Share this question
or