columns and columnDefs not working together

columns and columnDefs not working together

smasonsmason Posts: 25Questions: 11Answers: 0
edited February 2020 in Free community support

[Datatables 1.10.18]
Given the following snippet does not work. I get an error that says : Cannot set property '_DT_CellIndex' of undefined TypeError: Cannot set property '_DT_CellIndex' of undefined

let resultTable = $('table#results').DataTable( {
                    "dom": 'Bfrtip',                
                    "columns": [
                        { "name": "colPlant"     },
                        { "name": "colPartNum"   },
                        { "name": "colStatus"    },
                        { "name": "colCustomers" },
                        { "name": "colComments"  },
                        { "name": "colAction"    }
                    ],
                    "columnDefs": [{ "bSortable": false,
                                      "targets": [ "colCustomers", "colComments"]}
                    ]
                });

I'm trying to set the names of the columns so I can use those names in the targets to make updating in the future easier.

Meanwhile, this works:

        let resultTable = $('table#results').DataTable( {
                            "dom": 'Bfrtip',                     
                            "columnDefs": [{ "bSortable": false, "targets": [ 3, 4]} ]
        });

Can anyone advise what I'm doing wrong? Is the syntax wrong? Can you use columns and columnDefs in the datatable instantiation like that or are you supposed to use them separately?

This question has an accepted answers - jump to answer

Answers

  • smasonsmason Posts: 25Questions: 11Answers: 0

    I figured it out, I had one too many column names. I removed the bogus column and it works now.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    edited February 2020 Answer ✓

    According to the columnDefs.targets docs you can supply one of the following:

    0 or a positive integer - column index counting from the left
    A negative integer - column index counting from the right
    A string - class name will be matched on the TH for the column (without a leading .)
    The string "_all" - all columns (i.e. assign a default)

    Since you are providing a string its looking for a class name. This, I suspect, is why you are getting the _DT_CellIndex' of undefined error. You can assign a class with the columns.className option.

    The columns.name is used with Datatables API's to select the column. It is not intended for use with options.

    Kevin

This discussion has been closed.