How to sum the total count for each datatable column

How to sum the total count for each datatable column

comicragecomicrage Posts: 12Questions: 2Answers: 0
edited March 2013 in General
HI,

I got a datatable fill with either 1 or blank. The IQueryable object was originally Y or N. Just before I bind the data to the datatable, I changed it to 1 or blank with <%# Item.DataField == "Y" ? "1" : "" %>

At the bottom of the datatable, I need to add a footer row Total: 23 24 23, which the number is the sum of each columns ie adding all the 1's.

I look at the following example with fnDrawCallBack. In the sum+= line, I am not sure about the ._aData.{your item}. What is "your item"?

Any help is greatly appreciated.


$(document).ready(function () {

var oResultGrid = $("[id$='gvReportData']");
if (fixEmptyDataRow(oResultGrid)) {
var oTable = oResultGrid.dataTable({
"bPaginate": false,
"bFilter": false,
"bInfo": false,
"fnDrawCallback": function (oSettings) {
// Get all rows of your table
var nTrs = $tableElement.find('tbody tr');
// Enumerate all rows
var sum = 0;
for (var i = 0; i < nTrs.length; i++) {
var iDisplayIndex = oSettings._iDisplayStart + i;
var dataIndex = oSettings.aiDisplay[iDisplayIndex];
sum += oSettings.aoData[dataIndex]._aData.{your item};
}
// Insert sum at the end
$tableElement.find(' tbody tr:last').after(''+sum+'');
});
}
else {
oResultGrid.dataTable({
"sPaginationType": "full_numbers",
"aaSorting": [[1, 'asc']],
"fnDrawCallback": function (oSettings) {
// Get all rows of your table
var nTrs = $tableElement.find('tbody tr');
// Enumerate all rows
var sum = 0;
for (var i = 0; i < nTrs.length; i++) {
var iDisplayIndex = oSettings._iDisplayStart + i;
var dataIndex = oSettings.aiDisplay[iDisplayIndex];
sum += oSettings.aoData[dataIndex]._aData.{your item};
}
// Insert sum at the end
$tableElement.find(' tbody tr:last').after(''+sum+'');
});
}
});

function fixEmptyDataRow(table) {
// Check Contents of 1st TD tag in table. If it contains the EmptyDataRows text, then this is an Empty Data Row
var value = $(table).children('tbody').children('tr').children('td:first').text().trim();
// Prepend THEAD required by JQuery DataTables for the EmptyDataRow Table
if (value == "No results match your query") {
// EmptyDataItems template
table.prepend("");
return true;
}
else
return false;
}
This discussion has been closed.