【发布时间】:2019-02-22 10:35:12
【问题描述】:
我使用 JavaScript 返回一个商店服务员列表,并在最后一行(服务员)之后动态插入一行,显示总计列下所有服务员的总数。但现在我需要做的是在运行总计列中显示每个服务员的运行总计。
这就是我要显示的内容,显然运行总计公式不在这里......
我知道有一种更 Angular-y 的方式来做到这一点。我肯定会实施这种方式。但是现在,我只想让你专注于我正在走的路,关于计算这个运行的总列。以下是我希望您关注的具体内容:
var total = 0;
document.querySelectorAll("td:nth-child(4)").forEach((cell, index) => {
this.attendantNames.forEach(a=>{
this.total += a.total;
});
cell.innerText = this.total;
});
但如果你需要查看整个控制器代码,就在这里:
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'];
【问题讨论】:
标签: javascript angularjs json html-table