How to get the number of pages in DataTables

2019-05-16 20:51发布

问题:

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?

回答1:

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.');
    }
});