I am currently working with JavaScript in the context of an AngularJS application, attempting to insert a row at the bottom of a table that shows the total sum of a specific column.
Here is the code snippet I am using:
var table = document.querySelector('.table');
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cellData = document.createTextNode('Total ' + '$' + this.totals);
cell1.appendChild(cellData);
row.appendChild(cell1);
Unfortunately, using insertRow(-1) does not seem to work as expected. The only way I have been able to see the new row is by passing in zero as the first parameter, but then the row gets inserted into the table header.
Below is my complete code snippet:
import { digest, showLoader } from 'act/services/events';
import 'act/components';
import Searcher from 'act/services/lists/searcher';
import * as moment from 'moment';
import * as api from '../services/totals';
import {header, dev} from 'act/services/logger';
import {goToError} from 'act/services/controller-helpers';
import '../components/store-total';
const defaultStartDate = moment().startOf('day');
export default class StoreTotalsController {
constructor() {
this.attendantNames = [];
this.stores = [];
this.emptyResult = true;
this.totals = 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(this.totals);
})
var table = document.querySelector('.table');
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cellData = document.createTextNode('Total ' + '$' + this.totals);
cell1.appendChild(cellData);
row.appendChild(cell1);
this.emptyResult = false;
this.errorMessage = null;
} else {
this.errorMessage = resp[0].error.name;
}
digest();
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'];