peace_love
peace_love

Reputation: 6471

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.

Upvotes: 1

Views: 2102

Answers (1)

Alexandre Elshobokshy
Alexandre Elshobokshy

Reputation: 10922

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/

Upvotes: 1

Related Questions