4

DataTable Image -> When i am trying to get header name like this.. then i got only first column name which is NAME... But i need to get all headers name. How to get all Header Name ?

this.api().columns().every( function ($i) {
   var column = this;
   var order = this.order();
   var title = this.column(order[0][$i]).header();
   var tt = $(title).html();
   console.log(tt);
})

4 Answers 4

9

Seems a little bit overcomplicated :

this.api().columns().every(function() {
  console.log( this.header().textContent )
})

will do the "trick".

Sign up to request clarification or add additional context in comments.

1 Comment

this also show hidden column. Neat :)
1

This worked out for me:

table.columns().header().map(d => d.textContent).toArray()

Comments

0
   Alert the name of the column for a cell that was clicked on:



var table = $('#example').DataTable();
    $('#example tbody').on( 'click', 'td', function () {
        var idx = table.cell( this ).index().column;
        var title = table.column( idx ).header();
        alert( 'Column title clicked on: '+$(title).html() );
    } );

Comments

0
var dataTableHeaderElements = $('#tableId').DataTable().columns().header();
var headers = [];
for (var i = 0; i< dataTableHeaderElements.length; i++) {
    headers.push($(dataTableHeaderElements[i]).text())    
}

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.