Dynamic headers with aoColumns and aoColumnDefs.

Dynamic headers with aoColumns and aoColumnDefs.

OromarOromar Posts: 2Questions: 2Answers: 0

Hi,

I've got a JSP with a Datatable declaration that is created with a x number of headers depending of a parameter given.

To do this, I set the parameters aoColumns and aoColumnDefs dynamically so I can construct the headers titles at the beggining of the method, like this:

var columns = [];
var columnsDefs = [];
var obj;
var objDefs;

switch (nivel) {
    case '0':
        obj = { sTitle: 'Reference' };
        columns.push(obj);
        obj = { sTitle: 'Description' };
        columns.push(obj);
        obj = { sTitle: 'ID' };
        columns.push(obj);
        objDefs = { "sClass": "text-wrap", "aTargets": [ 0, 1, 2 ] };
        columnsDefs.push(objDefs);
        break;
    case '1':
        obj = { sTitle: 'Reference' };
        columns.push(obj);
        objDefs = { "sClass": "text-wrap", "aTargets": [ 0 ] };
        columnsDefs.push(objDefs);
        break;

and my datatable init:

$('#datatable').dataTable( {
    "bServerSide": true,
    "bProcessing": true,
    "bDestroy": true,
    "sAjaxSource": "myServlet",
    "fnServerData": function (sSource, aoData, fnCallback ){
        aoData.push( { "name": "OP", "value": "MGR" } );
        $.ajax({
             'dataType': 'json',
             'type': 'GET',
             'url': sSource,
             'data': aoData,
             'success': function (code){
                 fnCallback(code);
        }});                
     },
    "iDisplayLength": RegNumber,
    "bSort": true,
    "bLengthChange": false,
    "bFilter": false,
    "bPaginate": false,
    "paging":   false,
    "scrollY": 200,
    "scrollX": false,
    "responsive": true,
    "oLanguage": {
        "sSearch": "Search: ",
        "oPaginate":{
            "sPrevious": "Last",
            "sNext": "Next",
            "sFirst": "First",
            "sLast": "Last"
        }
    },     
    "sPaginationType": "full_numbers",   
    "aoColumnDefs": columnsDefs ,    <------------- Fails second time 
    "aoColumns": columns,                  <-------------   
    "sScrollX": "100%",
    "sScrollY": value
});

All this code is in the same function.

My problem is at the second time I try to access de the datatable. The first time runs ok, no problem creating the datatable with both values, but the second time it seems like it's getting aoColumns values OK but not the aoColumnsDefs, because I see the error (TypeError: nThs[(i - iCorrector)]...).

Any help please?

This discussion has been closed.