Search code examples
jquerydatatables

How to get the number of pages in DataTables


I am working with DataTables and I need to know the current number of pages that a table holds (this certainly depends on the number of rows per page and the total number of rows and may change by user-action). Does anybody know how to access this value?


Solution

  • I believe iTotalPages is what you're after: http://datatables.net/plug-ins/api#fnPagingInfo

    $.fn.dataTableExt.oApi.fnPagingInfo = function(oSettings){
        return {
            "iTotalPages": oSettings._iDisplayLength === -1 ?
                0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
        };
    };    
    
    $('#example').dataTable({
        "fnDrawCallback": function(){
            alert('There are ' + this.fnPagingInfo().iTotalPages + ' in this table.');
        }
    });