Grid Grouping, group headers, footers
Grouping can be enabled or disabled for the whole grid by using the .Groupable(bool)
and can be set for each column by setting the Column.Groupable
Column.Group
- defines initial column grouped stateColumn.GroupRank
- group rank for this columnColumn.GroupRemovable
- (default is true) if set to false grouped column won't have the remove group button.ShowGroupedColumn
- default true, show grouped columnsGridModelBuilder.MakeHeader
- defines a function for creating the GroupHeaderGridModelBuilder.MakeFooter
- function for creating a group footer
GridDemo/GroupingContent.cshtml
@model AweCoreDemo.ViewModels.Input.GridDemoGroupingCfgInput
@(Html.Awe().Grid("GroupingGrid")
.Columns(
new Column{Bind = "Id", Width = 75},
new Column{Bind = "Person", Group = Model.PersonGrouped,
GroupRemovable = Model.PersonRem, Groupable = Model.PersonGroupable, GroupRank = Model.PersonRank},
new Column{Bind = "Food.Name", Group = Model.FoodGrouped,
GroupRemovable = Model.FoodRem, Groupable = Model.FoodGroupable, GroupRank = Model.FoodRank},
new Column{Bind = "Date", Width = 180},
new Column{Bind = "Price", Width = 80},
new Column{Bind = "Location"},
new Column{Bind = "Chef.FirstName,Chef.LastName", Prop = "ChefName", Header = "Chef", FooterClientFormat = ".(ChefCount)"}
)
.Height(450)
.ShowGroupBar(Model.ShowGroupBar)
.Groupable(Model.Groupable)
.ShowGroupedColumn(Model.ShowGroupedColumn)
.Parameter("collapsed", Model.Collapsed)
.Url(Url.Action("GetItems","GroupingGrid")))
Demos/Grid/GroupingGridController.cs
public class GroupingGridController : Controller
{
public IActionResult GetItems(GridParams g, bool collapsed)
{
var query = Db.Lunches.AsQueryable();
// passing MakeFooter and MakeHeader to customize header and add footer
var gmb = new GridModelBuilder<Lunch>(query, g)
{
KeyProp = o => o.Id,
Map = o => new
{
o.Id,
o.Person,
FoodName = o.Food.Name,
Date = o.Date.ToShortDateString(),
o.Price,
o.Location,
ChefName = o.Chef.FullName
},
MakeFooter = MakeFooter,
MakeHeader = gpinf => MakeHeader(gpinf, collapsed)
};
return Json(gmb.Build());
}
private GroupHeader MakeHeader(GroupInfo<Lunch> g, bool collapsed)
{
// get first item in the group
var first = g.Items.First();
// get the grouped column value(s) for the first item
var val = string.Join(" ", AweUtil.GetColumnValue(g.Column, first).Select(ToStr));
return new GroupHeader
{
Content = string.Format(" {0} : {1} ( Count = {2}, Max Price = {3} )",
g.Header,
val,
g.Items.Count(),
g.Items.Max(o => o.Price)),
Collapsed = collapsed
};
}
private object MakeFooter(GroupInfo<Lunch> info)
{
// will add the word Total at the grid level footer (Level == 0)
var pref = info.Level == 0 ? "Total " : "";
return new
{
Food = pref + " count = " + info.Items.Count(),
Location = info.Items.Select(o => o.Location).Distinct().Count() + " distinct locations",
Date = pref + " max: " + info.Items.Max(o => o.Date).Date.ToShortDateString(),
Price = info.Items.Sum(o => o.Price),
ChefCount = info.Items.Select(o => o.Chef.Id).Distinct().Count() + " chefs"
};
}
private static string ToStr(object o)
{
return o is DateTime ? ((DateTime)o).ToShortDateString() : o.ToString();
}
}
Comments