Home > Mobile >  How can I jump to a specific page in datatables?
How can I jump to a specific page in datatables?

Time:11-26

I try to go to a speific page on initComplete:

var table = $('#example').dataTable( {
  "initComplete": function( settings, json ) {
    table.page(5).draw(false);
  }
} );

But it is not working. My page is still at page 1.

CodePudding user response:

You have two issues:

  • You need to use .DataTable not .dataTable
    (specifically for the .page() call, not for the initial init)
  • At the time initComplete runs, .DataTable({}) has not returned, so table is undefined

This can be confirmed with:

  "initComplete": function (settings, json) {
    console.log(table);
    //table.page(5).draw(false);
  }

However, in initComplete, you can use this, so you don't need the table variable.

Changing to .DataTable and this and your code works fine:

$('#example').DataTable({
  "pageLength": 2,
  "initComplete": function(settings, json) {
    $(this).DataTable().page(5).draw(false);
  }
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>

<table id="example" >
  <thead>
    <tr>
      <th>Company name</th>
      <th>Address</th>
      <th>Town</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Emkay Entertainments</td>
      <td>Nobel House, Regent Centre</td>
      <td>Lothian</td>
    </tr>
    <tr>
      <td>The Empire</td>
      <td>Milton Keynes Leisure Plaza</td>
      <td>Buckinghamshire</td>
    </tr>
    <tr>
      <td>Emkay Entertainments</td>
      <td>Nobel House, Regent Centre</td>
      <td>Lothian</td>
    </tr>
    <tr>
      <td>The Empire</td>
      <td>Milton Keynes Leisure Plaza</td>
      <td>Buckinghamshire</td>
    </tr>
    <tr>
      <td>Emkay Entertainments</td>
      <td>Nobel House, Regent Centre</td>
      <td>Lothian</td>
    </tr>
    <tr>
      <td>The Empire</td>
      <td>Milton Keynes Leisure Plaza</td>
      <td>Buckinghamshire</td>
    </tr>
    <tr>
      <td>Emkay Entertainments</td>
      <td>Nobel House, Regent Centre</td>
      <td>Lothian</td>
    </tr>
    <tr>
      <td>The Empire</td>
      <td>Milton Keynes Leisure Plaza</td>
      <td>Buckinghamshire</td>
    </tr>
    <tr>
      <td>Emkay Entertainments</td>
      <td>Nobel House, Regent Centre</td>
      <td>Lothian</td>
    </tr>
    <tr>
      <td>The Empire</td>
      <td>Milton Keynes Leisure Plaza</td>
      <td>Buckinghamshire</td>
    </tr>
    <tr>
      <td>Emkay Entertainments</td>
      <td>Nobel House, Regent Centre</td>
      <td>Lothian</td>
    </tr>
    <tr>
      <td>The Empire</td>
      <td>Milton Keynes Leisure Plaza</td>
      <td>Buckinghamshire</td>
    </tr>
    <tr>
      <td>Emkay Entertainments</td>
      <td>Nobel House, Regent Centre</td>
      <td>Lothian</td>
    </tr>
    <tr>
      <td>The Empire</td>
      <td>Milton Keynes Leisure Plaza</td>
      <td>Buckinghamshire</td>
    </tr>
    <tr>
      <td>Emkay Entertainments</td>
      <td>Nobel House, Regent Centre</td>
      <td>Lothian</td>
    </tr>
    <tr>
      <td>The Empire</td>
      <td>Milton Keynes Leisure Plaza</td>
      <td>Buckinghamshire</td>
    </tr>
    <tr>
      <td>Emkay Entertainments</td>
      <td>Nobel House, Regent Centre</td>
      <td>Lothian</td>
    </tr>
    <tr>
      <td>The Empire</td>
      <td>Milton Keynes Leisure Plaza</td>
      <td>Buckinghamshire</td>
    </tr>
  </tbody>
</table>

CodePudding user response:

The problem with your code is that table is not defined in your callback context

The only thing i found in my little research is that you can get the table api from the settings parameter.

https://datatables.net/forums/discussion/34352/passing-datatable-object-to-initcomplete-callback

so maybe it could work like this:

var table = $('#example').DataTable( {
  "initComplete": function( settings, json ) {
    settings.oInstance.api().page(5).draw(false);
  }
});

I didnt try though

  • Related