Datatables fnAdjustColumnSizing doesn't work after ajax processing

11,892

Solution 1

I find a solution by using a function inside the "success" callback of my ajax query :

$.ajax( {
    "dataType": 'json', 
"type": "POST", 
"url": "webservice.php", 
"data": 'edit_quotation=true&action=true' + data, 
"success": function(msg){
    fnCallback(msg);
    $('.overlay').hide();
    adjustTable();
}
});

function adjustTable(){
    oTable.fnAdjustColumnSizing();
}

And it work like a charm but I don't know why. Someone can explain ?

Solution 2

Have you tried doing

"fnInitComplete": function() {
    oTable.fnAdjustColumnSizing();
}

Because i'm not sure that this points to table object

Share:
11,892
Awea
Author by

Awea

I’m a French developer who practice ruby on rails, php and a little bit of cocoa. I have started to develop two years ago for fun and now I work for a web agency as web developer.

Updated on June 15, 2022

Comments

  • Awea
    Awea almost 2 years

    I'm currently using datatables with ajax data and I want do adjust the column width. So I found this function fnAdjustColumnSizing and I try to use it :

    oTable = $('.datatable').dataTable( {
        "sScrollX": "100%",
        "sScrollXInner": "200%",
        "bScrollCollapse": true,
        "bDestroy" : true,
        "sAjaxSource": "xhr.php",
        "bFilter": false,
        "bSort": false,
        "bLengthChange": false,
        "bPaginate": false,
        "bInfo": false,
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.ajax( {
                "dataType": 'json', 
                "type": "POST", 
                "url": "webservice.php", 
                "data": 'id=' + quotation_id + '&customer_id=' + id + '&action=true', 
                "success": function(msg){
                    fnCallback(msg);
                }
            });
        },
        "fnInitComplete": function() {
            this.fnAdjustColumnSizing();
        }
    });
    

    The function haven't any effect but if I use it inside another event such like this :

    $('#target').click(function() {
        oTable.fnAdjustColumnSizing();
    });
    

    It work well, any idea ?

  • Awea
    Awea over 12 years
    There is a way to use fnAdjustColumnSizing after the oTable assignation ?