Group Total / grand total in grid
As per demo grid total or count showing page wise ... Not group wise or sub group wise. Every page it is giving page wise count / total in footer ...
How to get that group total / grand total
SK
asked
at 04 Jun 2020
Answers
-
In this demo: https://demo.aspnetawesome.com/GridDemo/Grouping you can see the totals being calculated in the
MakeFooter
function, the calculations in the demo are being made usinginfo.Items
which contains the items for the current group in the current page, you could ignoreinfo.Items
and calculate the Total directly from the unpaged data source.info.Column
contains the name of the current Groupgp.Groups
will contain all the groups in order, so using this info you could query your data source and calculate the unpaged group total. you could use Dynamic Linq for that https://dynamic-linq.netMakeHeader = GetMakeHeader(g),
...
private Func<GroupInfo<Lunch>, GroupHeader> GetMakeHeader(GridParams gp)
{
// getting the aggregate values irrespective of how many items we are rendering in the current page
GroupHeader Func(GroupInfo<Lunch> info)
{
// get first item in the group
var first = info.Items.First();
// get the grouped column value(s) for the first item
var val = string.Join(" ", AweUtil.GetColumnValue(info.Column, first).Select(ToStr));
var query = Db.Lunches.AsQueryable();
foreach (var groupName in gp.Groups)
{
var names = groupName.Split(','); // for grouping by Chef column Chef.FirstName,Chef.LastName
for (var i = 0; i < names.Length; i++)
{
var name = names[i];
var gvals = AweUtil.GetColumnValue(groupName, first).ToArray();
query = query.Where(name + "==@0", gvals[i]); // using Dynamic Linq
}
if (groupName == info.Column) break;
}
return new GroupHeader { Content = string.Format(" {0} : {1} ( Count = {2} )", info.Header, val, query.Count()) };
}
return Func;
}Omuanswered at 04 Jun 2020