DataTables: Cannot read property style of undefined

前端 未结 11 641
时光取名叫无心
时光取名叫无心 2020-12-23 15:42

I am getting this error with the following:

jquery.dataTables.js:4089 Uncaught TypeError: Cannot read property \'style\' of undefined(…)
_fnCalculateColumnWi         


        
相关标签:
11条回答
  • 2020-12-23 16:06

    POSSIBLE CAUSES

    • Number of th elements in the table header or footer differs from number of columns in the table body or defined using columns option.
    • Attribute colspan is used for th element in the table header.
    • Incorrect column index specified in columnDefs.targets option.

    SOLUTIONS

    • Make sure that number of th elements in the table header or footer matches number of columns defined in the columns option.
    • If you use colspan attribute in the table header, make sure you have at least two header rows and one unique th element for each column. See Complex header for more information.
    • If you use columnDefs.targets option, make sure that zero-based column index refers to existing columns.

    LINKS

    See jQuery DataTables: Common JavaScript console errors - TypeError: Cannot read property ‘style’ of undefined for more information.

    0 讨论(0)
  • 2020-12-23 16:06

    Funnily i was getting the following error for having one th-/th pair too many and still google directed me here. I'll leave it written down so people can find it.

    jquery.dataTables.min.js:27 Uncaught TypeError: Cannot read property 'className' of undefined
    at ua (jquery.dataTables.min.js:27)
    at HTMLTableElement.<anonymous> (jquery.dataTables.min.js:127)
    at Function.each (jquery.min.js:2)
    at n.fn.init.each (jquery.min.js:2)
    at n.fn.init.j (jquery.dataTables.min.js:116)
    at HTMLDocument.<anonymous> (history:619)
    at i (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at Function.ready (jquery.min.js:2)
    at HTMLDocument.K (jquery.min.js:2)
    
    0 讨论(0)
  • 2020-12-23 16:06

    In my case, I was updating the server-sided datatable twice and it gives me this error. Hope it helps someone.

    0 讨论(0)
  • 2020-12-23 16:07

    Make sure that in your input data, response[i] and response[i][j], are not undefined/null.

    If so, replace them with "".

    0 讨论(0)
  • 2020-12-23 16:11

    You said any suggestions wold be helpful, so currently I resolved my DataTables "cannot read property 'style' of undefined" problem but my problem was basically using wrong indexes at data table initiation phase's columnDefs section. I got 9 columns and the indexes are 0, 1, 2, .. , 8 but I was using indexes for 9 and 10 so after fixing the wrong index issue the fault has disappeared. I hope this helps.

    In short, you got to watch your columns amount and indexes if consistent everywhere.

    Buggy Code:

        jQuery('#table').DataTable({
            "ajax": {
                url: "something_url",
                type: 'POST'
            },
            "processing": true,
            "serverSide": true,
            "bPaginate": true,
            "sPaginationType": "full_numbers",
            "columns": [
                { "data": "cl1" },
                { "data": "cl2" },
                { "data": "cl3" },
                { "data": "cl4" },
                { "data": "cl5" },
                { "data": "cl6" },
                { "data": "cl7" },
                { "data": "cl8" },
                { "data": "cl9" }
            ],
            columnDefs: [
                { orderable: false, targets: [ 7, 9, 10 ] } //This part was wrong
            ]
        });
    

    Fixed Code:

        jQuery('#table').DataTable({
            "ajax": {
                url: "something_url",
                type: 'POST'
            },
            "processing": true,
            "serverSide": true,
            "bPaginate": true,
            "sPaginationType": "full_numbers",
            "columns": [
                { "data": "cl1" },
                { "data": "cl2" },
                { "data": "cl3" },
                { "data": "cl4" },
                { "data": "cl5" },
                { "data": "cl6" },
                { "data": "cl7" },
                { "data": "cl8" },
                { "data": "cl9" }
            ],
            columnDefs: [
                { orderable: false, targets: [ 5, 7, 8 ] } //This part is ok now
            ]
        });
    
    0 讨论(0)
  • 2020-12-23 16:13

    I had this issue when i set colspan in table header. So my table was:

            <thead>
                <tr>
                    <th colspan="2">Expenses</th>
    
                    <th colspan="2">Income</th>
    
                    <th>Profit/Loss</th>
                </tr>
            </thead>
    

    Then once i change it to:

            <thead>
                <tr>
                    <th>Expenses</th>
                    <th></th>
                    <th>Income</th>
                    <th></th>
                    <th>Profit/Loss</th>
                </tr>
            </thead>
    

    Everything worked just fine.

    0 讨论(0)
提交回复
热议问题