headerCallback reading data issue

headerCallback reading data issue

postitiefpostitief Posts: 22Questions: 9Answers: 0

Hello all,

I'm using the headerCallback function to modify the header of the datatable after the data is received from a AJAX call. The specific data I need is in the latest row of the data array. So I hoped to be able to this:

$('#example').dataTable( {
  ajax: urlToAjaxFile.php,
  "headerCallback": function(thead, data, start, end, display){
    grandTotal = data[end-1][1];
    $(thead).find('th').eq(1).html(grandTotal);
  }
});

But that unfortunately does not work and I get an error:
Uncaught TypeError: Cannot read property '1' of undefined

So I ended up doing this, which is working, but completely stupid!

$('#example').dataTable( {
  ajax: urlToAjaxFile.php,
  "headerCallback": function(thead, data, start, end, display){
    for(var i=0; i<data.length; i++){
      grandTotal = data[end-1][1];
    }
    $(thead).find('th').eq(1).html(grandTotal);
  }
});

How can this be done in a better way?

This discussion has been closed.