Buttons destroy function bug?

Buttons destroy function bug?

dnguyendnguyen Posts: 1Questions: 0Answers: 0
edited July 2016 in Free community support

I recently updated my older versions of Data Tables and Table Tools to the newer versions - DataTables 1.10.12, and Buttons 1.2.1. In my code, I often call on the data tables destroy() function if a table node already exists with a certain tag, which as I understand then calls all the destroy() functions of any additional plug-ins used with Data Tables.

I found that when I try to create a new table in that same table node with 1 button, it works fine. However, when I try to create multiple buttons, it fails, saying that it "cannot read property node of undefined". Using Chrome developer tools and the console, I found the issue was within the Buttons plug-in itself when the destroy method is called:

for ( i=0, ien=buttons.length ; i<ien ; i++ ) {
        this.remove( buttons[i].node );
}

Because the button node itself is being removed, the rest of the buttons end up shifting in position to an index that's one smaller each iteration, causing it to hit a state where a button at some position is "undefined".
When I changed that code to:

for ( i=buttons.length-1, ien=0 ; i>=ien ; i-- ) {
        this.remove( buttons[i].node );
}

My buttons all work! Something felt a little strange to me about going into the Buttons plug-in JavaScript and having to change the code there to make it work... Has anyone else experienced a similar issue and can propose a different fix?? Or is this a bug that was never found?

Here's a link to an example with the problem recreated: http://jsfiddle.net/q4yje715/

Thanks in advance!!

Replies

This discussion has been closed.