"TypeError: Cannot read properties of undefined (reading 'buttons') at ce.fn.init.initComplete (

"TypeError: Cannot read properties of undefined (reading 'buttons') at ce.fn.init.initComplete (

cantthinkofonerightnowcantthinkofonerightnow Posts: 7Questions: 3Answers: 0
edited December 2023 in Free community support

Link to test case: https://live.datatables.net/filipiwa/1/edit
Debugger code (debug.datatables.net):
Error messages shown: TypeError: Cannot read properties of undefined (reading 'buttons') at ce.fn.init.initComplete
Description of problem: When I call the data from a page via AJAX the page works but when I use a local variable I get TypeError

Hello, I'm trying to create a test case for several issues I have with my table, since I cannot use the original data I tried creating a local JSON variable. For some reason, I'm getting this error when I use that.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,328Questions: 26Answers: 4,949
    Answer ✓

    The error is due to this line in initComplete:

    table.buttons().container().appendTo($('div.toolbar'));
    

    The table variable you are assigning using the Datatables init code is not always ready/assigned at the time initComplete executes. You can get an instance of the API using this.api() within initComplete, for example:

        initComplete: function () {
          var api = this.api();
    
          // initComplete code
    
          api.buttons().container().appendTo($('div.toolbar'));
        }
    

    You reference this.api() in other locations of initComplete. You can assign the variable as the first line then change all the references to use the variable.

    Updated example:
    https://live.datatables.net/tawadifu/1/edit

    Kevin

  • cantthinkofonerightnowcantthinkofonerightnow Posts: 7Questions: 3Answers: 0

    @kthorngren, thank you! This is greatly appreciated!

Sign In or Register to comment.