Cannot reinitialise DataTable

Cannot reinitialise DataTable

mgalemgale Posts: 11Questions: 1Answers: 0

Not sure what I've done .,.. this was working fine. Then I moved all files to a new site(on the same server) and now get an error.

The "can't initialize" error alert shows "DataTables warning: table id=DataTables_Table_0" - so it is the first (and only) dataTable on the page. When the alert is dismissed, the dataTable functions fine - with all the parameters in place.

This occurs on all pages where I have a dataTable (so it's not limited to an error on one table).

All pages with a dataTable table are called in the same way, using the same js file.

Table initialization is called in the html for the table:

<table class='table table-bordered hover dataTable'>

```Error occurs ONLY if I put any parameters in the initialization. If there are no parameters, I don't get the error.

CAUSES ERROR:

$('table.dataTable').dataTable({ 
    stateSave: true,
    "order": [[0, "desc"]],
    pagingType: "full_numbers",
    lengthMenu: [[25, 50, 100, 200, 500, -1], [25, 50, 100, 200, 500, "All"]],
    dom: 'liBftip',
    buttons: [
        {
            extend: 'colvis',
            postfixButtons: ['colvisRestore']
        },
        'copyHtml5', 
        {
            extend: 'csvHtml5',
            fieldSeparator: '\t',
            extension: '.csv'
        }
    ]              
});

CAUSES ERROR:

$('table.dataTable').dataTable({ });
```
DOES NOT CAUSE ERROR
$('table.dataTable').dataTable();

Example page with the error is here: http://www.marie-dev.com/dataTables-Example.html

I was not able to get the debugger to run - there appears to be some conflict with the error alert. (It runs fine when the alert doesn't come up).

Any help would be appreciated.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi mgale,

    The reason is because you're first initialising the DataTable in that large code block with several customisations. But, when you use $('table.dataTable').dataTable({ });, the {} is saying initialise again, but this time with no customisations - this would be in conflict with the first.

    And as you said, $('table.dataTable').dataTable(); works because it isn't initialising, it's just using what's already been initialised already...

    Hope that makes sense.

    Your fix would be to just use that second one.

    Cheers,

    Colin

  • mgalemgale Posts: 11Questions: 1Answers: 0

    I am not using all three - just tested to see what works. I need to initialize it with the customizations, but when I do, I get the error.

    Odd thing is, that same code block has been working for a long time ... it's something else that changed, probably when I moved the files. But I can't tell what is causing the conflict.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited February 2018

    script src='includes/js/dataTables/datatables.min.js'
    script src='includes/js/dataTables/jquery.dataTables.min.js'

    You don't need both of those.

  • mgalemgale Posts: 11Questions: 1Answers: 0

    Well, that seemed to solve it! Thanks.

This discussion has been closed.