fnPageChange is not a function

Viewed 474

I am getting the subject error when using the following:

 $("#example1").DataTable().fnPageChange(localStorage.getItem('page'),true);

From what I understand, it saves the last page of the datatable that the user clicked on. If the user has clicked on the 3rd page of the datatable, navigates to another page and then goes back to the datatable, it will still be on the 3rd page of the datatable.

And it works. The last page of the datatable is saved.

The only problem is, even though the above works, I am getting the following error in the console:

Uncaught TypeError: $(...).DataTable(...).fnPageChange is not a function

I am not sure why.

Here are some other datatable setting I have:

"iDisplayLength": 50,
"order": [[ 1, "desc" ]],
"paging": true,
"scrollY": 300,
"scrollX": true,
"bDestroy": true,
"stateSave": true,
"sPaginationType":"full_numbers",
"autoWidth": false,
"deferRender": true,
"dom": 'Bfrtip',

Why am I getting the above console error and how can I fix it?

2 Answers

Your use of the "stateSave": true option is what is allowing a user to return to page 3 of the DataTable, after leaving the web page entirely, and then returning.

It also saves filters and sorting selections.

If you want to do something manually, which is not handled for you by "stateSave": true, then you can use the DataTables page change event. The documentation gives an example:

$('#example').on( 'page.dt', function () {
    var info = table.page.info();
    $('#pageInfo').html( 'Showing page: '+info.page+' of '+info.pages );
} );

Otherwise, you can delete that line which is throwing the error - and state saving should not be affected.


Regarding fnPageChange - this is an old plug-in which has been removed from DataTables - you can see a note about this here.


DataTable começa com minúsculo é um camel case.
 $("#example1").DataTable().fnPageChange(localStorage.getItem('page'),true);

change to

$("#example1").dataTable().fnPageChange(localStorage.getItem('page'),true);
Related