4
votes

I'm using the Datatables.net plugin for JQuery. I am trying to create a "Totals" row in the footer. I'm using this example as a reference: http://datatables.net/release-datatables/examples/advanced_init/footer_callback.html

Does the array aiDisplay in this example give all the Rows indexes that are currently displayed? Or all the ones the fulfill the current filters?

To clarify the difference here would be the rows that fulfill the filter, but are not on the current page. There may be 30 rows that fulfill the filter, but only 10 of those rows are displayed because pagination is set to 10.

If aiDisplay contains all the rows fulfilling the filter regardless of pagination then great! However, if it literally does contain only the rows show on the current page, is there a way I can get an array of all the rows that fulfill the filter?

Thanks!

As an aside: It seems as if this array should contain all indexes fulfilling the filter, not just the ones on the page. Since the iStart and iEnd values are provided the rows on the page can be derived and providing an array with only the values on the page provides no additional value over provide all the values that fulfill the filter. However I could find no documentation on this.

1
Couldn't you check this for yourself with a bit of test coding?Ben Barden

1 Answers

6
votes

I did a check about that function, so let me give you an explanation. nRow actually is not important. Just ignore it. The aaData contains all the data in the table, including the data that is excluded by your filter. iStart and iEnd I think you know. They are the index of rows to be displayed. The aiDisplay actually contains all the (filtered) data, though some of it may not be displayed due to pagination. You can also use this function. It has its own logic that you can change to achieve want you want.

$('#example').dataTable({
    "fnFooterCallback": function ( nRow, aaData, iStart, iEnd, aiDisplay ) {
        /*
        * Calculate the total market share for all browsers in this table (ie inc. outside
        * the pagination)
        */
        var iTotalMarket = 0;
        for ( var i=0 ; i<aaData.length ; i++ ){
            iTotalMarket += aaData[i][4]*1;
        }

        /* Calculate the market share for browsers on this page */
        var iPageMarket = 0;
        for ( var i=iStart ; i<iEnd ; i++ ){
            iPageMarket += aaData[ aiDisplay[i] ][4]*1;
        }

        /* Modify the footer row to match what we want */
        var nCells = nRow.getElementsByTagName('th');
        nCells[1].innerHTML = parseInt(iPageMarket * 100)/100 +
                '% ('+ parseInt(iTotalMarket * 100)/100 +'% total)';
    }
});