An error mesg when displaying more columns than there are in table [with FIX]

An error mesg when displaying more columns than there are in table [with FIX]

zdanekzdanek Posts: 1Questions: 0Answers: 0

Hello.
I've created a little fix, but stumbled upon that there's no source code on github and I have to put a fix here.
So here it goes:

<table id="ordersTable>
<thead>
<tr>
<th>id</th> <th>requestDate</th>
</tr>
</thead>
</table>

using Ajax with a response with an array of Objects that have more fields and all those fields are setup in request:

$('#ordersTable').DataTable(
    {
        "ajax" : {
            "url": '/api/clients',
            "dataSrc": ''
        },
        columns: [
            { data: 'id' },
            { data: 'requestDate' },
            { data: 'clientId' }
               ]
     });

suddenly we have an error in browser console:

jquery.dataTables.js:5604 Uncaught TypeError: Cannot read property 'style' of undefined

this is because it has 3 visibleColumns but there are 2 headerColumns. So my fix is to be put on line 5597:

            if (headerCells.length < visibleColumns.length) {
                _fnLog( oSettings, 0, 'Declared visible columns [' + visibleColumns.length + '] > displayed table header cells [' + headerCells.length + ']');
            }

Replies

  • allanallan Posts: 64,311Questions: 1Answers: 10,621 Site admin

    Thanks for this. The DataTables source is here.

    However, what you are describing is not actually a bug, but a configuration error. The columns array should exactly match the number of HTML columns in the table. Anything else is a configuration error. In this case, if you don't want clientId to be shown in the table, just don't include it in the list of column definitions.

    Allan

This discussion has been closed.