Using the DataSource component, developers can easily aggregate data and retrieve the results of aggregation. As with other operations, aggregation can
be performed automatically by the DataSource or the control can be used to pass the needed aggregate descriptors and get the aggregation done on the
server.
Define Aggregates
The aggregate
property of the component specifies an object or an array of objects that is used to describe the aggregations that need to be made on the next data
read. These objects should define the following options:
field: The name of the field that needs to be aggregated.
aggregate: The aggregate function. Accepted values are: "average",
"count", "max", "min", and
"sum".
Once the data is read and the aggregates are calculated, the
aggregates
property is used to retrieve the result of aggregation
in the form of a series of nested objects with keys equal to the data fields and aggregate functions.
Aggregating DataSource Data | Copy |
---|
var myDataSource = new Telerik.Data.DataSource({
data: [
{ orderId: 1, price: 10.23 },
{ orderId: 2, price: 14.30 },
{ orderId: 3, price: 123.41 },
{ orderId: 4, price: 45.10 }
],
aggregate: [
{ field: "orderId", aggregate: "count" },
{ field: "price", aggregate: "sum" }
]
});
myDataSource.read().then(function (data) {
var orderIdCount = myDataSource.aggregates.orderId.count;
var totalPrice = myDataSource.aggregates.price.sum;
});
|
Server Aggregates
When bound to remote data, the Boolean serverAggregates property defines how aggregation is performed. If the value
is set to true, the DataSource sends aggregate descriptors to the server and expects the aggregated data as part of the
result. Note that based on the scenario, you may need to use the transport.parameterMap property to
modify the aggregation info to the remote endpoint. For more information, see the Configuring Remote Binding
article.
The default value of the serverAggregates property is false, meaning data is aggregated locally inside the DataSource.