jquerydatatablescolumndefinition

How can I return the current column name in columnDefs jquery datatables?


Like this I can return the value of the current row:

"columnDefs": [

  {
    "render": function (data, type, row) {
     return data ;
 },

What I am actually looking for is the name of the current column. I tried:

"columnDefs": [

  {
    "render": function (data, type, row) {
     return column().name ;
 },

But this did not work.


Solution

  • If you specify a targets in your columnDefs you could do the following by adding a meta parameter :

    "columnDefs": [
        {
            targets: 0,
            "render": function (data, type, row, meta) {
                var title = $('#example').DataTable().columns( meta.col ).header(); 
                var columnName = $(title).html();
                return columnName;
            }
        },
    ]
    

    JSFiddle example (check the log) : https://jsfiddle.net/1jot32nz/