Finally, after lots of R&D on the footer total row, I found that we can implement PinnedBottomRow
to show our total for the table.
So I dropped the above idea as it's creating a problem with the pinned columns and also managing 2 tables is tricky.
Thanks to AreYouSiries who provided such a nice demo on plucker here
Also Thanks to Ag-Grid for such a nice doc with live examples
My Custom Plucker version for Total Row is here
By following the above examples I am able to achieve my exact requirements and now it's working as expected.
Let me add sort steps to achieve the total row feature in ag-grid:
1st step - Generate Pinned Total Row: Below function will generate an empty target object with all your columns available in the grid.
generatePinnedBottomData(){
// generate a row-data with null values
let result = {};
this.gridColumnApi.getAllGridColumns().forEach(item => {
result[item.colId] = null;
});
return this.calculatePinnedBottomData(result);
}
2nd step Calculate Total for some or all the columns: You need to calculate the total from row data and set the value in the above generated targeted row.
calculatePinnedBottomData(target: any){
//console.log(target);
//**list of columns fo aggregation**
let columnsWithAggregation = ['age']
columnsWithAggregation.forEach(element => {
console.log('element', element);
this.gridApi.forEachNodeAfterFilter((rowNode: RowNode) => {
//if(rowNode.index < 10){
//console.log(rowNode);
//}
if (rowNode.data[element])
target[element] += Number(rowNode.data[element].toFixed(2));
});
if (target[element])
target[element] = `Age Sum: ${target[element].toFixed(2)}`;
})
//console.log(target);
return target;
}
3rd and last step: Call above generatePinnedBottomData()
function where u get your grid data from API or local DB. In the above example, we used to call from onGridReady()
onGridReady(params) {
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
console.log(this.gridColumnApi.getAllGridColumns())
this.http
.get("https://raw.githubusercontent.com/ag-grid/ag-grid/master/packages/ag-grid-docs/src/olympicWinners.json")
.subscribe(data => {
this.rowData = data;
setTimeout(()=>{
let pinnedBottomData = this.generatePinnedBottomData();
this.gridApi.setPinnedBottomRowData([pinnedBottomData]);
}, 500)
});
}
You need to assign generated data to the grid.
That's it now you can see your total row pinned at bottom of the grid.
Final Output:
Hope my post will help you to achieve the total row in the grid.