Undefined is not an object (evaluating 'n[q].style')

Undefined is not an object (evaluating 'n[q].style')

jaxonjaxon Posts: 1Questions: 1Answers: 0

Howdy
I just started using DataTables and I constantly running in exceptions. Now there's one I just don't get - since 6 hours, trying everything...

jQuery throws a TypeError: undefined is not an object (evaluating 'n[q].style')

This is my javascript:

var tableSettings = function(config){
    var columns = [];
    for (var columnIndex = 0; columnIndex < config.columns.length; columnIndex++) {
        var columnData = config.columns[columnIndex];
        columns.push({
            bSortable:true,
            sName: columnData.name
        });
    }
    
    return {
        dom: '<"top">rt<"bottom"iflp><"clear">',
        bFilter: false,
        bSort: true,
        bServerSide: true,
        bStateSave: true,
        sAjaxSource: config.table.ajax,
        bProcessing: true,
        scrollX: true,
        initComplete: function() {
            var table = $('#'+config.table.id).DataTable();
            if (table.page.info().start > table.page.info().recordsTotal) {
                table.draw();
            }
        },
        iDisplayLength: 10,
        aoColumns: columns
    }
};

$('#'+config.table.id).dataTable(tableSettings(config));

and here my config:

var config = {
        targetId: 'categories',
        table: {
            id: 'categoryTable',
            class: 'redTable',
            ajax: '/Category/GetCategoryList'
        },
        columns: [
            {
                name: 'id'
            },
            {
                name: 'title'
            }
        ]
    };

Anyone any idea/hint for me?

Answers

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922

    I don't see anything that stands out as an issue with the code above. Although I'm not sure what the purpose of the initComplete code is. If the if statement is true then the table.draw() will execute but, at this point, it has already drawn.

    In order to help debug we will need a link to your page or a test case replicating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • mindguardmindguard Posts: 3Questions: 1Answers: 0
    edited May 2020

    Hi.
    Maybe I can add some example to this mystery, as I observed something similar as well:

    http://live.datatables.net/vulihuyi/1/edit

    When recreating a DataTable with a different header, destroy() does not seem to work: In the example the table should be created a second time. But it isn't. And one of the errors is: "undefined is not an object..."
    However, after manually removing thead and tbody it works. Or did I misunderstand the idea of destroy() ?

    All the best
    mindguard

  • kthorngrenkthorngren Posts: 21,171Questions: 26Answers: 4,922

    This is from the destroy() docs:

    This method can be used to remove those enhancements and return the table to its original un-enhanced state, with the data shown in the table.

    You will need to write the code to manipulate the HTML elements as needed.

    Kevin

This discussion has been closed.