user1758908
user1758908

Reputation: 146

Render the column header in datatables

I am trying to render the column header in a datatables cell. So far I am using columnDefs to adjust the contents of a cell, and I wish to add the column header or title:

"columnDefs": [ {
    "targets": [6], "render": function (data, type, column) {
        return '?continent='+column[1]+'?filter='+this.header;
     }
}]

I have been able to return the column header in the console or as an alert: (https://datatables.net/reference/api/column().header())

but not as text in the cell

Fiddle: http://jsfiddle.net/bbLjzspf/3481/ I would like the header of column 1 - Position - to be inserted into the link where I have written Col1header. Result for row 1 would look like: "tom.html?office=Tokyo?filter=Position"

Upvotes: 2

Views: 7296

Answers (1)

Bhumi Shah
Bhumi Shah

Reputation: 9476

Here is the solution:

https://codepen.io/creativedev/pen/bKEPNe

This is columnDefs code which i have changed

"columnDefs": [ {
    "targets": [1], 
    "render": function (data, type, row, meta) {
        var title = $('#example').DataTable().columns( meta.col ).header(); 
        var columnName = $(title).html();
        var url = 'tom.html?office='+row[2]+'?filter='+columnName.toLowerCase();
       return '<a href="'+url.trim()+'">'+data+'</a>';
     }
}]

Also, added "deferRender": true, which will render once dataloaded

Upvotes: 2

Related Questions