Problems initiating DT buttons in function executed via initComplete

Problems initiating DT buttons in function executed via initComplete

jLinuxjLinux Posts: 981Questions: 73Answers: 75
edited October 2015 in Free community support

I have a DT instance where the DataTables is initialized after an AJAX call, so I have most of the DT related functions execute via the initComplete.

I was getting an error locally, the error was: Uncaught TypeError: Cannot read property 'inst' of undefined at datatables.js:91554 (Unminified)

My stack trace shows that the line in my code thats causing this is line in which I use buttons(), this is the simplified snippet:

function manage_buttons($dt) {
    var $api = $dt.api();
    
    new $.fn.dataTable.Buttons( $api, {
        buttons: [ {text: 'Copy', extend: 'copy'} ]
    } );
    
    // THIS line
    $api.buttons( 1, null ).container().appendTo( '#export-assets' );
}

I was able to re-produce the error on the DataTables JSbin --> HERE (Use your browsers JS console to see the error, here is a screenshot of it)

Any help would be greatly appreciated

P.S: I know the code is laid out rather odd, but I did it like that because thats how it is in my script. The do_datatable() gets executed via an $.ajax promise.

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    P.S. I was able to get the exact same error by removing the manage_buttons(this) out of the initComplete and moving it to just below the entire initialization code of the DataTables instance, I used manage_buttons($assets_dt), then altered the manage_buttons function to accept the API as an argument, but got the exact same error.

    So I know its not related to the manage_buttons getting executed inside the initComplete

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    Usually I find the answer within an hour of posting on the forums, this ones kinda got me stumped though. >.<

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    Thanks to @ThomD, I was able to change my script around, and because of that, it seems to work now. I got it to work inside of JSBin as well (HERE).

    EDIT: Oh my god, im ready to pull my hair out!!!! So as I stated above, I was able to resolve it in the JSbin, but I made the same changes in my local code, but I still get the exact same error!! >.<

    If anyone can take a look at the original JSBin script and maybe see what was up, that would be awesome.. Thanks!

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    I think I solved it. It looks like it was an issue with the first parameter in the buttons() (groupSelector), I didnt have it starting at 0, I had it starting at 1. Once I changed all of the groupSelector values to -1 of whatever it was, they all worked.

    Very odd, since it worked before without a problem..

    P.S. Yep, confirmed, I solved it.

    I hate that I cant delete my own threads. I wish people could delete their own threads within X hours of creating it, if there are no comments/replies to it.. lol

This discussion has been closed.