console error "Cannot read property mData of undefined"..??


Hello, I am using jQuery datatable (little bit modified as in theme) in my project and getting error as below

Uncaught TypeError: Cannot read property 'mData' of undefined
    at HTMLTableCellElement.<anonymous> (jquery.dataTables.min.js:91)
    at Function.each (jquery-1.11.1.min.js:2)
    at m.fn.init.each (jquery-1.11.1.min.js:2)
    at HTMLTableElement.<anonymous> (jquery.dataTables.min.js:91)
    at Function.each (jquery-1.11.1.min.js:2)
    at m.fn.init.each (jquery-1.11.1.min.js:2)
    at m.fn.init.m [as dataTable] (jquery.dataTables.min.js:84)
    at HTMLDocument.<anonymous> (Details?CustomerProfileID=101088&pv=False:1316)
    at j (jquery-1.11.1.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-1.11.1.min.js:2)

Here is my javascript code:

  var table = $('#dataTable');

        var settings = {
            "sDom": "<'table-responsive't><'row'<p i>>",
            "destroy": true,
            "autoWidth": false,
            "scrollCollapse": true,
            "oLanguage": {
                "sLengthMenu": "_MENU_ ",
                "sInfo": "Showing <b>_START_ to _END_</b> of _TOTAL_ entries"
            },
           "iDisplayLength": 5 
        };
 table.dataTable(settings);

it works on other pages/table but not working for one table, what can be the cause of the issue, not able to understand it, my table html looks proper with <tbody> & <thead> tags


Asked by:- pika
2
: 17599 At:- 1/2/2018 12:55:00 PM
jQuery jQuery-datatable Cannot read property mData of undefined

Check if number of columns are same in header and body, if your table has tbody and thead tag already then this could be the issue 0
By : vikas_jk - at :- 1/5/2018 10:17:37 AM






2 Answers
profileImage Answered by:- manish

These can be the possible issues related to this error :

1.Table must contain <thead> and <tbody> tags, otherwise it throws this error, for example below table will throw error

<table id="erroTable">
    <tr>
        <th>Test</th>
        <th>Test2</th>
    </tr>
    <tr>
        <td>Data-1</td>
        <td>Data-2</td>
    </tr>
</table>

<!--Error may occur because of no tbody and thead defined-->

2. Mismatched number of columns(this table will also throw error) :

<table id="ErrorTable">
    <thead>
        <tr>
            <th>Test</th>
            <th>Test2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Ok-1</td>
            <td>ok-2</td>
            <td>Error because of this extra value</td>
        </tr>
    </tbody>
</table>

3.It can also occur if you have table arguments for 'aoColumns':[..] which don't match the correct number of columns, means may be you have defined aoColumns for 4 columns but there are only 2 columns in table.

4.Could occur due to Duplication of table Id(two table with same Ids)

Anyone of the above solutions should work.

3
At:- 1/2/2018 2:34:02 PM Updated at:- 7/26/2022 2:50:22 PM
My error was "2. Mismatched number of columns(this table will also throw error) ", thanks 0
By : pika - at :- 1/10/2018 8:31:28 AM
"Mismatched number of columns" solution, solved my error too, thanks. 0
By : jon - at :- 10/4/2018 1:40:44 PM


profileImage Answered by:- bhanu

Most of the times, this error occurs when number of columns in <thead> <th> is different than <tbody> <tr> <td>

But it can also occur if number of columns in 'aoColumns':[ ] does not have the correct number of columns.

For example, this configuration will not work

<table id="datatable">
    <thead>
        <tr>
            <th>col 1</th>
            <th>col 2</th>
        </tr>
    </thead>
    <tbody>
        <td>data 1</td>
        <td>data 2</td>
    </tbody>
</table>
<script>
        var datTable = $('#datatable');
        datTable.DataTable({
            'order': [[ 1, 'desc' ]],
            'aoColumns': [
                null,
                null, 
                null,
                null,
                {
                    'bSortable': false
                }
            ]
        });
</script>

But this will work:

<table id="datatable">
    <thead>
        <tr>
            <th>col 1</th>
            <th>col 2</th>
        </tr>
    </thead>
    <tbody>
        <td>data 1</td>
        <td>data 2</td>
    </tbody>
</table>
<script>
        var datTable = $('#datatable');
        datTable.DataTable({
            'order': [[ 1, 'desc' ]],
            'aoColumns': [
                null,
                {
                    'bSortable': false
                }
            ]
        });
</script>

Hope this helps.

1
At:- 5/19/2021 8:36:59 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use