Grouping & Aggregators
index
Demo
Regular Grouping
Draggable Grouping
Description
Fully dynamic and interactive multi-level grouping with filtering and aggregates that is achieved with the help of the DataView
object in SlickGrid
. Each grouping level can have its own aggregates (over child rows, child groups, or all descendant rows). An aggregate can be seen as sub-totals, totals, average, ... or any defined group(s).
How does it work in SlickGrid
? The important thing to understand while working with SlickGrid
is that Grouping requires you to provide 2 things, if you omit 1 of them, it will simply not work. These 2 things are
You will need to define which type of aggregate (accumulator) you want to use
Angular-Slickgrid provides the following built-in
Aggregators
:Avg
,Min
,Max
,Sum
You need to add a
groupTotalsFormatter
on the column definition you want it to be calculatedthis is very similar to a Formatter, except that they are designed to show aggregate results, e.g::
Total: 142.50$
These 2 steps go hands in hands, a groupTotalsFormatter
would have nothing to show if it does not have an Aggregator
.
Setup
One of the very first thing that you need to do is to provide the SlickGrid DataView
object to your ViewModel
. The DataView
is where we will define all of our Grouping. You can get the dataView
object through an Event Emitter onDataviewCreated
like so:
View
Component
Draggable Dropzone Location
The Draggable Grouping can be located in either the Top-Header or the Pre-Header as described below.
Pre-Heaader
Draggable Grouping can be located in either the Pre-Header of the Top-Header, however when it is located in the Pre-Header then the Header Grouping will not be available (because both of them would conflict with each other). Note that prior to the version 8.1 of Angular-Slickgrid, the Pre-Header was the default and only available option.
Top-Heaader
requires v8.1 and higher
This is the preferred section since the Top-Header is on top of all headers (including pre-header) and it will always be the full grid width. Using the Top-Header also frees up the Pre-Header section for the potential use of Header Grouping.
When using Draggable Grouping and Header Grouping together, you need to enable both top-header and pre-header.
Aggregators
The Aggregators
is basically the accumulator, the logic that will do the sum (or any other aggregate we defined). We simply need to instantiate the Aggregator
by passing the column definition field
that will be used to accumulate. For example, if we have a column definition of Cost and we want to calculate it's sum, we can call the Aggregator
as follow
The available built-in Aggregators
are
Aggregators.Avg
(calculate the Average of a group)Aggregators.Min
(returns the Minimum value of a group)Aggregators.Max
(returns the Maximum value of a group)Aggregators.Sum
(calculate the Sum of a group)Aggregators.Clone
(will clone the same grouped text and display it in as an aggregated value)Aggregators.Distinct
(will show distinct value)
Group Totals Formatter
When defining your column definitions, you will need to decide which of the column will have an aggregate. Once that decision is made, you will add a groupTotalsFormatter
to that column definition in question (a Formatter for the group total). For example, let say that we have a cost and we want a total sum grouped by a duration, the code would look like below.
Available Group Total Formatters
You can see the full list under groupingFormatters.index.ts
Note: the Group Total Formatters named as currency will have these extra params
(groupFormatterPrefix
, groupFormatterSuffix
, groupFormatterCurrencyPrefix
, groupFormatterCurrencySuffix
) and also the other common Formatter params
(minDecimal
, maxDecimal
, decimalSeparator
, thousandSeparator
, displayNegativeNumberWithParentheses
).
avgTotalsPercentageFormatter
avgTotalsDollarFormatter
avgTotalsCurrencyFormatter
avgTotalsFormatter
minTotalsFormatter
maxTotalsFormatter
sumTotalsColoredFormatter
sumTotalsCurrencyFormatter
sumTotalsCurrencyColoredFormatter
sumTotalsDollarColoredBoldFormatter
sumTotalsDollarColoredFormatter
sumTotalsDollarBoldFormatter
sumTotalsDollarFormatter
sumTotalsFormatter
sumTotalsBoldFormatter
ViewModel
Prefix/Suffix to a groupTotalsFormatter
We can also pass prefix and/or suffix to each groupTotalsFormatter
by adding them to the params
object. Also note that you can also type HTML to be interpreted. For example, let say we would like Total:
to show as bold and a suffix of 'USD' , you can write it this way:
Note prefix/suffix are concatenated without spaces, if you require a space then make sure to add it in accordingly.
ViewModel
Custom groupTotalsFormatter
groupTotalsFormatter
You can also create a custom groupTotalsFormatter
similarly to a Formatter, just a create a function that will return a string, for example:
ViewModel
Set a Grouping
Once you have added a groupTotalsFormatter
and defined which aggregate you want to use, you will want to create a grouping function. If we take again our example of a grid with multiple task and we want to group our task by duration and calculate the duration average and the cost total sum, we can write the following function
ViewModel
Clear Grouping / Collapse All / Expand All
To "Clear all Grouping", "Collapse all Groups" and "Expand all Groups", we can simply call the associated DataView
function, like so:
ViewModel
Styling (change icons)
The current icons are chevron (right/down), however if you wish to use +/- icons. You can simply update the SASS variables to use whichever SVG icon paths. The SASS variables you can change are
For more info on SASS styling and variables, please read the Wiki - SASS Styling,
Last updated