Update header cell item, representing column sum, when a filter finishes running
Update header cell item, representing column sum, when a filter finishes running
Hello I am trying to generate a new sum of all the values in a column whenever a user filters the column data. I think I have most of the code I need and am trying to use the fnHeaderCallback to trigger the work. Where I'm stuck at is passing the correct column index for the column data I am interested in summing. Here's the code I have thus far:
[code]
// this is the callback
"fnHeaderCallback": function (nHead, aasData, iStart, iEnd, aiDisplay) {
calculateColumnSum(???); // how do I pass my desired column's index here?
}
// this function does the actual calculation
function calculateColumnSum(colIndex) {
// get row count for looping later
var tableRowCount = $('#SymbolsGrid tr').length;
// get col index to calc sum from
var firstColumnSelectedVal = colIndex;
var operator = "+";
var allResults = '';
var currentTotal = 0;
// skip first 3 rows, which are headers and titles
// loop through all rows and sum total
for (var i = 3; i < tableRowCount; i++) {
var currentCellVal = $('#SymbolsGrid tr:eq(' + i + ') td:eq(' + firstColumnSelectedVal + ')').text();
if (operator != 'Operation' && Number(currentCellVal) != NaN) {
// create math formula to do the sum calculation
var finalFormula = currentCellVal + operator + currentTotal;
// check for invalid characters
finalFormula = finalFormula.replace(/\,/g, "");
finalFormula = finalFormula.replace("00.", "0.");
finalFormula = finalFormula.replace(/\%/g, "");
allResults += finalFormula + "\r\n";
// do the eval
var answer = eval(finalFormula).toString();
currentTotal = answer;
// shorten decimal places for looks
var dotLength = answer.indexOf('.'); // get index of .
if (dotLength != -1) {
var lengthOfDecimalPlaces = answer.length - dotLength; // get length after .
var desiredLength = 0;
// if more than 4 decimal places set desiredLength to dot + 4
if (lengthOfDecimalPlaces > 4) {
desiredLength = dotLength + 4;
}
else {
desiredLength = answer.length;
}
answer = answer.substring(0, desiredLength);
}
}
}
// set value of sum cell in header
$('#SymbolsGrid tr:eq(2) td:eq(' + firstColumnSelectedVal + ')').text(currentTotal);
}
[/code]
[code]
// this is the callback
"fnHeaderCallback": function (nHead, aasData, iStart, iEnd, aiDisplay) {
calculateColumnSum(???); // how do I pass my desired column's index here?
}
// this function does the actual calculation
function calculateColumnSum(colIndex) {
// get row count for looping later
var tableRowCount = $('#SymbolsGrid tr').length;
// get col index to calc sum from
var firstColumnSelectedVal = colIndex;
var operator = "+";
var allResults = '';
var currentTotal = 0;
// skip first 3 rows, which are headers and titles
// loop through all rows and sum total
for (var i = 3; i < tableRowCount; i++) {
var currentCellVal = $('#SymbolsGrid tr:eq(' + i + ') td:eq(' + firstColumnSelectedVal + ')').text();
if (operator != 'Operation' && Number(currentCellVal) != NaN) {
// create math formula to do the sum calculation
var finalFormula = currentCellVal + operator + currentTotal;
// check for invalid characters
finalFormula = finalFormula.replace(/\,/g, "");
finalFormula = finalFormula.replace("00.", "0.");
finalFormula = finalFormula.replace(/\%/g, "");
allResults += finalFormula + "\r\n";
// do the eval
var answer = eval(finalFormula).toString();
currentTotal = answer;
// shorten decimal places for looks
var dotLength = answer.indexOf('.'); // get index of .
if (dotLength != -1) {
var lengthOfDecimalPlaces = answer.length - dotLength; // get length after .
var desiredLength = 0;
// if more than 4 decimal places set desiredLength to dot + 4
if (lengthOfDecimalPlaces > 4) {
desiredLength = dotLength + 4;
}
else {
desiredLength = answer.length;
}
answer = answer.substring(0, desiredLength);
}
}
}
// set value of sum cell in header
$('#SymbolsGrid tr:eq(2) td:eq(' + firstColumnSelectedVal + ')').text(currentTotal);
}
[/code]
This discussion has been closed.
Replies