Destroy method throwing an error every other time

Destroy method throwing an error every other time

The Destroy method fails every second time I call it. I'm utilizing fixed columns too. Here's my table initialization. By the way, when it works (first time, third time, etc.), everything seems to operate correctly.

            table = $('#pivotGrid').DataTable({
                "dom": 'T<"clear">lfrtip',
                "tableTools": {
                    "sSwfPath": '/includes/js/datatables/extensions/tabletools/swf/copy_csv_xls_pdf.swf',
                    "aButtons": [
                        {
                            "sExtends": "csv",
                            "sButtonText": "Save to CSV"
                        },
                        {
                            "sExtends": "copy",
                            "sButtonText": "Copy to Clipboard"
                        }
                    ]
                },
                "scrollY": 400,
                "scrollX": true,
                "scrollcollapse": true,
                "paging": false,
                "data": rows,
                "columnDefs": [{ "targets": 1, "visible": false }],
                "columns": columns,
                "order": [[1, "asc"], [2, gBlnSortDirection]], //ordinal, then total column
                "orderFixed": {
                    "pre": [1, "asc"], //always sort asc on this column
                    "post": [2, gBlnSortDirection]
                }, 
                "fnInitComplete": function () {
                    $('#pivotGrid tbody tr').each(function () {
                        $(this).find('td:eq(0)').attr('nowrap', 'nowrap');
                        $(this).find('th:eq(0)').attr('nowrap', 'nowrap');
                    });
                }
            });

When I click the run button and get new data I'm using this code to destroy an existing datatable (also using fixed columns extension) inside of a try-catch.

            if ($.fn.DataTable.isDataTable('#pivotGrid')) {
                table = $('#pivotGrid').DataTable();
                table.destroy();
                $('#pivotGrid').empty();
            }

Destroy method eventually calls "iterator: function ( flatten, type, fn, alwaysNew )" on line 6807 of jquery.dataTables.js. Inside that function there is this code (I added the try-catch so I could set a debug point). The fn.call is failing every other time it runs (runs once ok, runs second time, fails).

        for ( i=0, ien=context.length ; i<ien ; i++ ) {
            var apiInst = new _Api( context[i] );

            if (type === 'table') {
                try {
                                    ret = fn.call( apiInst, context[i], i );
                } catch (e) {
                    throw e;
                }

                if ( ret !== undefined ) {
                    a.push( ret );
                }
            }

Here is the error. Obviously some object is undefined. But what?

"Cannot read property 'appendChild' of undefined"

Anyone have an idea of what I should look for to fix the problem?

Replies

  • sciszewski@salesapex.comsciszewski@salesapex.com Posts: 10Questions: 0Answers: 0

    Here's the stack trace where the error occurs

    "TypeError: Cannot read property 'appendChild' of undefined
    at Object.FixedColumns._fnClone ([somewhere]/datatables.fixedcolumns.js:997:20)
    at Object.FixedColumns._fnCloneLeft ([somewhere]/datatables.fixedcolumns.js:901:8)
    at Object.FixedColumns._fnDraw ([somewhere]/datatables.fixedcolumns.js:836:8)
    at HTMLTableElement.<anonymous> ([somewhere]/datatables.fixedcolumns.js:555:10)
    at HTMLTableElement.m.event.dispatch ([somewhere]/jquery-1.11.1.min.js:3:8436)
    at HTMLTableElement.r.handle ([somewhere]/jquery-1.11.1.min.js:3:5146)
    at Object.m.event.trigger ([somewhere]/jquery-1.11.1.min.js:3:7537)
    at HTMLTableElement.<anonymous> ([somewhere]/jquery-1.11.1.min.js:3:15404)
    at Function.m.extend.each ([somewhere]/jquery-1.11.1.min.js:2:2973)
    at m.fn.m.each ([somewhere]/jquery-1.11.1.min.js:2:835)"

  • sciszewski@salesapex.comsciszewski@salesapex.com Posts: 10Questions: 0Answers: 0

    Found the problem and where the error originates. Not sure why it's doing what it's doing. Anyone know this code?
    in the datatables.fixedcolumns.js file line ~991

    "_fnClone": function ( oClone, oGrid, aiColumns, bAll )
    {
    var that = this, i, iLen, j, jLen, jq, nTarget, iColumn, nClone, iIndex, aoCloneLayout, jqCloneThead, aoFixedHeader;

        /*
         * Header
         */
        if ( bAll )
        {
            if ( oClone.header !== null )
            {
                oClone.header.parentNode.removeChild( oClone.header );
            }
            oClone.header = $(this.dom.header).clone(true, true)[0];
            oClone.header.className += " DTFC_Cloned";
            oClone.header.style.width = "100%";
            oGrid.head.appendChild( oClone.header );
    
            /* Copy the DataTables layout cache for the 
            header for our floating column */
            aoCloneLayout = 
                this._fnCopyLayout( this.s.dt.aoHeader, aiColumns );
    
            //----------------PROBLEM-------------------------//
            jqCloneThead = $('>thead', oClone.header);
            //-------------------------------------------------------//
    
            jqCloneThead.empty();
    
            /* Add the created cloned TR elements to the table */
            for ( i=0, iLen=aoCloneLayout.length ; i<iLen ; i++ )
            {
                //--------------------THROWS ERROR-----------------------//
                jqCloneThead[0].appendChild( aoCloneLayout[i].nTr );
            }
    
  • sciszewski@salesapex.comsciszewski@salesapex.com Posts: 10Questions: 0Answers: 0
    edited December 2014

    Found the problem and where the error originates. Not sure why it's doing what it's doing. Anyone know this code?

    Found in the datatables.fixedcolumns.js file line ~991

    _fnClone: function ( oClone, oGrid, aiColumns, bAll )
    {
    var that = this, i, iLen, j, jLen, jq,
    nTarget, iColumn, nClone, iIndex,
    aoCloneLayout, jqCloneThead, aoFixedHeader;

        /*
         * Header
         */
        if ( bAll )
        {
            if ( oClone.header !== null )
            {
                oClone.header.parentNode.removeChild( oClone.header );
            }
            oClone.header = $(this.dom.header).clone(true, true)[0];
            oClone.header.className += " DTFC_Cloned";
            oClone.header.style.width = "100%";
            oGrid.head.appendChild( oClone.header );
    
            /* Copy the DataTables layout cache for the 
            header for our floating column */
            aoCloneLayout = 
                this._fnCopyLayout( this.s.dt.aoHeader, aiColumns );
    
            //----------------PROBLEM-------------------------//
            jqCloneThead = $('>thead', oClone.header);
            //-------------------------------------------------------//
    
            jqCloneThead.empty();
    
            /* Add the created cloned TR elements to the table */
            for ( i=0, iLen=aoCloneLayout.length ; i<iLen ; i++ )
            {
                //--------------------THROWS ERROR-----------------------//
                jqCloneThead[0].appendChild( aoCloneLayout[i].nTr );
            }
    
  • allanallan Posts: 61,822Questions: 1Answers: 10,129 Site admin

    Can you link to a test page showing the issue, as required in the forum rules, so it can be debugged please?

    This example shows a table with FixedColumns being created and destroyed several times without issue.

    Allan

  • sciszewski@salesapex.comsciszewski@salesapex.com Posts: 10Questions: 0Answers: 0

    I can try to do that. But I was trying different things last night and I can definitely tell you the bug appears when I utilize the fixedcolums extension and goes away when I comment it out. It seems like the clone method of fixedcolums is failing when it's being called as a side effect of the destroy call (I'm assuming the draw on the table is in turn calling draw for the extension).

    It works every other time because when it fails on the second report run the table doesn't exist anymore which means the third time I run the report bypasses the destroy call.

  • sciszewski@salesapex.comsciszewski@salesapex.com Posts: 10Questions: 0Answers: 0

    Bypassed the issue by removing this from the table intitalization.
    '''
    "columnDefs": [{ "targets": 1, "visible": false }],
    '''

    Apparently FixedColumns has an issue with this. I just don;t have any more time to debug it, but where I highlighted the problem and the error are still where the issue is.

  • allanallan Posts: 61,822Questions: 1Answers: 10,129 Site admin

    Thanks for the information. Updating my example with that code does indeed show the error: http://live.datatables.net/wazufevo/3/edit .

    I'll try to debug it later on.

    Allan

  • triplethreat77triplethreat77 Posts: 4Questions: 4Answers: 0

    Has this been fixed? I am having the same issue, and have been working on a fix for a while.

  • allanallan Posts: 61,822Questions: 1Answers: 10,129 Site admin

    Not yet sorry. Not had time yet. Will try to make some as soon as I can.

    Allan

  • lazarzlazarz Posts: 1Questions: 0Answers: 0

    What seems to solve the problem is omitting the keys (for columns which are supposed to be hidden) from the "aoColumns" and "aaData" properties in initialization. For this to work, those keys must be at the end of the object's key list (or JSON being parsed), that is no other key for a visible column can come after. Hope this helps.

  • Katrina_EngelbrechtKatrina_Engelbrecht Posts: 3Questions: 0Answers: 0

    Has this been fixed? I am having the same issue, and have been working on a fix for a while.

  • allanallan Posts: 61,822Questions: 1Answers: 10,129 Site admin

    Running the example above with the currently nighty versions appears to run without issue.

    Allan

This discussion has been closed.