DataTables / DataTablesSrc

DataTables source repository
https://datatables.net
MIT License
587 stars 422 forks source link

Column definitions and hidden columns don't work together #261

Closed miken32 closed 4 months ago

miken32 commented 4 months ago

After upgrading to 2.0 I'm unable to use a column definition on the same table as a hidden column.

new DataTable('#example', {
    columns: [
        { data: 'name' },
        { data: 'position' },
        { data: 'office' },
        { data: 'start_date' },
        { data: 'salary' },
        { data: 'id'},
        { data: 'extn', visible: false},
    ],
    ajax: "/ajax/objects.txt",
    columnDefs: [
      {targets: '.id'}
    ]
});
      <table id="example" class="display nowrap" width="100%">
        <thead>
          <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Start date</th>
            <th>Salary</th>
            <th class="id"></th>
          </tr>
        </thead>
        <tbody>
        </tbody>
      </table>

Live code example is here: https://live.datatables.net/rebozoyu/1/ Commenting out either the hidden column or the column definition will render the table correctly, otherwise an error is thrown. There is a loop over the columns object that assumes the table will have the same number of headers I think? https://github.com/DataTables/DataTablesSrc/blob/master/js/core/core.columns.js#L457

AllanJard commented 4 months ago

Thank you for the test case! Fix committed and it will be in 2.0.2.