问题描述:

I'm using the Aggregates feature of a Kendo UI Grid Widget to display the sum of a grouped column. The column in question is bound to an optional field / property so when the data set includes a data item where property is not present (i.e. undefined), the sum ends up being NaN as you would expect.

The Kendo DataSource calculates these aggregates "when the data source populates with data" but does not include a feature to allow custom aggregates that would allow me to implement a version of sum that substitutes 0 for undefined values.

I can see where the sum aggregate function is defined in kendo.data.js but I would prefer not to change that if possible.

I have an idea how to solve this by writing a function to query the $("#myGridId").data("kendoGrid").dataSource but I'd like to know if there is a better option.

网友答案:

After comparing the latest code in kendo.data.js to my project version (2013.1.319) I see that they have changed the implementation of the sum aggregate function to handle this case by only performing the addition if the value is a number. Problem solved if I can get the project updated to the latest version of Kendo UI.

The code snippet below is at line 1369 of version 2014.1.416.

sum: function(accumulator, item, accessor) {
    var value = accessor.get(item);
    if (!isNumber(accumulator)) {
        accumulator = value;
    } else if (isNumber(value)) {
        accumulator += value;
    }

    return accumulator;
}
相关阅读:
Top