sums of columns with ColReorder and ColVis

sums of columns with ColReorder and ColVis

bioPorcobioPorco Posts: 14Questions: 0Answers: 0
edited March 2011 in General
i'm using dataTable with ColReorder, ColVis and TableTools in addition i want to put in the footer of table the sum of some columns,
i've tryed to use fnFooterCallback but i have two problem:
1- first parameter of function ('TR' element for the footer) is always null.
2- when I change position af a column fnFooterCallback isn't invoked, I suppose because no 'darw' event is launched
and one question:
1- How can i know which columns are visible and which ones are hidden into the fnFooterCallback function


I thought the first problem arises because I use JSON datasource and dataTable doesn't generated footer automatically
then I append manually the section with following code
[code]
$('#example').append('.....');
[/code]
but the problem persist.

Instead, for second problem,I have no ideas to solve it.

Thanks

Replies

  • bioPorcobioPorco Posts: 14Questions: 0Answers: 0
    edited March 2011
    Ops....
    i've resolved first and second problem was sufficient to place the following code
    [code]
    $('#example').append('.....');
    [/code]
    before this code
    [code]
    dataTableOpt={ 'aaData':[...], 'aoColumns':[...], ...}
    $('#example').dataTable(dataTableOpt);
    [/code]

    now the question is how to know which columns are visible and which ones are hidden into the fnFooterCallback function?

    the aaData parameter contains the data of all rows and all columns of table, even if i hidden some columns.
    this is a problem, because if the first column was hidden, the sum of the first column appears in the leftmost footer of the table which is related to second column.

    this is my fnFooterCallback:
    [code]
    var sumsCallback = function (nRow, aaData, iStart, iEnd, aiDisplay) {
    var sums = new Array();
    for (i = iStart; i < iEnd; i++) {
    for (j = 0; j < aaData[aiDisplay[i]].length; j++) {
    var num = parseFloat(aaData[aiDisplay[i]][j]);
    if (!isNaN(num)) {
    if (sums[j]) {
    sums[j] += num;
    } else {
    sums[j] = num;
    }
    }
    }
    }
    var cells = nRow.getElementsByTagName('th');
    for (var i = 0; i < cells.length; i++) {
    if (sums[i]) {
    cells[i].innerHTML = 'Tot: ' + sums[i] + '';
    } else {
    cells[i].innerHTML = '';
    }
    }
    }
    [/code]
  • MikeSMikeS Posts: 113Questions: 1Answers: 0
    I'm particularly interested in seeing a solution to your problem #2. Like you, I use the table footer to show summary values for numeric columns (Min, Max, Avg, etc). All works well for me until columns are reordered and/or hidden.

    It would be a huge benefit (to me at least; being a noob at jquery/script) if someone could create and post an "ultimate datatables sample". A sample that shows off all the bells and whistles of this fantastic tool. Currently, I have to dig thru each of the existing examples to see if I could scavenge any snippets or hints of doing what I need and most of the time I can't figure it out anyway :(
  • bioPorcobioPorco Posts: 14Questions: 0Answers: 0
    i've found a solution about how to know which columns are visible and which ones are hidden into the fnFooterCallback function

    [code]
    var sumsCallback = function (nRow, aaData, iStart, iEnd, aiDisplay) {
    var sums = new Array();
    for (i = iStart; i < iEnd; i++) {
    for (j = 0; j < aaData[aiDisplay[i]].length; j++) {
    var num = parseFloat(aaData[aiDisplay[i]][j]);
    if (!isNaN(num)) {
    if (sums[j]) {
    sums[j] += num;
    } else {
    sums[j] = num;
    }
    }
    }
    }
    for(var i=0;i
This discussion has been closed.