Using JavaScript I am returning a list of store attendants and dynamically inserting a row after the last row (attendant) displaying the total for all attendants under the total column. But now what I need to do is to display a running total, for each attendant, in the running total column.
Here's what I'm displaying, clearly the running totals formula isn't right here...
I know there is a more Angular-y way to do this. And I will implement that way for sure. But for now, I'd just like you to focus on the path I'm heading down, regarding calculating this running total colum. And here specifically is what I'd like you to focus on:
var total = 0;
document.querySelectorAll("td:nth-child(4)").forEach((cell, index) => {
this.attendantNames.forEach(a=>{
this.total += a.total;
});
cell.innerText = this.total;
});
But if you need to see the whole controller code, it's right here:
export default class StoreTotalsController {
constructor() {
this.attendantNames = [];
this.stores = [];
this.emptyResult = true;
this.totals = 0;
this.total = 0;
this.cellValue = "";
this.runningTotalCells;
//this.previousTotal = 0;
}
getAttendants() {
showLoader('Searching');
const baseUrl = '/src/areas/store-totals/services/tender-total-data.json';
const getStores = new Request(baseUrl, {
method: 'GET'
});
fetch(getStores).then(function(response){
return response.json();
}).then(resp => {
if (!(resp[0] && resp[0].error)) {
this.attendantNames = resp.stores[0].attendants;
this.attendantNames.forEach(a=>{
this.totals += a.total;
console.log("Attendant :" , a.attendantName, "Had a total of :" , a.total, "With running total " , this.totals);
});
//Row at bottom displaying total of totals
var table = document.querySelector('.table');
var td1 = document.createElement('td');
var td2 = document.createElement('td');
var td3 = document.createElement('td');
var tr = document.createElement("tr");
tr.setAttribute("class", "last-row");
var td1Data = document.createTextNode(' ');
var td2Data = document.createTextNode('Total');
var td3Data = document.createTextNode('$' + this.totals.toFixed(2));
td1.appendChild(td1Data);
td2.appendChild(td2Data);
td3.appendChild(td3Data);
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
table.appendChild(tr);
this.emptyResult = false;
this.errorMessage = null;
} else {
this.errorMessage = resp[0].error.name;
}
digest();
var total = 0;
document.querySelectorAll("td:nth-child(4)").forEach((cell, index) => {
this.attendantNames.forEach(a=>{
this.total += a.total;
});
cell.innerText = this.total;
});
showLoader(false);
});
}
searchIfReady() {
if (this.search && this.date && this.date.isValid()) {
this.getSearch();
}
}
updateDate(date) {
this.date = moment(date).startOf('day');
this.searchIfReady();
}
}
StoreTotalsController.$inject = ['$stateParams'];
