Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SlickGrid Aggregation/ Sum of a column

I am simply trying to sum a column and display the total at the bottom of the table. From looking at examples it seems like using an aggregator is the best way to do this. However when I simply add in

dataView.setAggregators([ new Slick.Data.Aggregators.Sum("value") ], false);

nothing happens. I have been trying for hours to just get a simple aggregator working based off of the grouping example but the grouping example is too complex for me to be able to tell exactly what I need.

Edit: Alternate fix: For anyone else looking I ended up not using aggregators. The data is available in Javascript which makes life a lot easier. Totals can be computed from there.

like image 397
Ryan Avatar asked Oct 08 '22 14:10

Ryan


1 Answers

First of all please note that displaying total works only for sub-total (the sub-total of the group you have), I do not believe the Grand Total is yet implemented... Though if you do want the sub-total of each group, you have to define 3 area of code, not just 1

1st you declare the aggregator like you previously done

dataView.setAggregators([ new Slick.Data.Aggregators.Sum("value") ], false);

2nd you need to attach it (a function to display) to your column as well with groupTotalsFormatter

var columns = [
  ...
  {id: "cost", name: "Cost", field: "cost", width: 90, sortable: true, groupTotalsFormatter: sumTotalsFormatter}
];

finally define the function to display it and code it with what you want to display

function sumTotalsFormatter(totals, columnDef) {
  var val = totals.sum && totals.sum[columnDef.field];
  if (val != null) {
    return "total: " + val;
  }
  return "";
}

If you're missing any of these lines, you will not see anything displayed and that is normal

like image 200
ghiscoding Avatar answered Oct 12 '22 09:10

ghiscoding