API and DOM initialise

API and DOM initialise

cornacumcornacum Posts: 5Questions: 2Answers: 0

Hi

I am running Datatables and I have managed to find a way to run API to change "no. of Entries" shown, and also I am using DOM to replace in which order the elements are shown around the table.

I run these codes individually and they work, however, when I use them both, I get the - Cannot reinitialise DataTable error.

API code:
** var table = $('#datatable').DataTable();
table.page.len( 50 ).draw();**

DOM code:
**$('#datatable').dataTable( {
"dom": 'tip'

      } ); 

**

Error:
"DataTables warning: table id=datatable - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3"

From the message, I understand that I can only initialise the tables once, but I am not sure how to write this in a code?

Any help would be appreciated.

Thanks

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922
    edited March 2023 Answer ✓

    Are you executing this code:

    var table = $('#datatable').DataTable();
    table.page.len( 50 ).draw();
    

    Before this code?

    $('#datatable').dataTable( {
    "dom": 'tip'
    } );
    

    If so the first one is initializing the Datatable with default options. The second one has init options so Datatables wants to initialize itself but that has already happened, That is why the error occurs.

    You could swap the two code snippets and it would work.

    Better would be to use the pageLength option and do this:

    var table = $('#datatable').DataTable({
      pagelength: 50,
      "dom": 'tip'
    });
    

    Kevin

  • cornacumcornacum Posts: 5Questions: 2Answers: 0

    I tried this and the initialise error is not showing any more, but there is no effect on page length, still remains at default 10, but the "dom" does work.

  • cornacumcornacum Posts: 5Questions: 2Answers: 0

    Correction

    After multiple page refreshes, it does work.

    Thank you Kevin!

Sign In or Register to comment.