最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Datatables fnAdjustColumnSizing doesn't work after ajax processing - Stack Overflow

programmeradmin0浏览0评论

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 ?

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 ?

Share Improve this question edited Jan 2, 2012 at 14:12 Reporter 3,9365 gold badges35 silver badges49 bronze badges asked Jan 2, 2012 at 14:10 AweaAwea 3,1838 gold badges42 silver badges60 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

Have you tried doing

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

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

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 ?

I had a similar problem in Internet Explorer 8 (not in Firefox): I was getting my headers unaligned with respect to the table body.

The table is initialized inside a 'modal' dialog (twitter bootstrap), AFTER it is shown. Finally, to make it work with Internet Explorer 8, after creating the table I'm making this call:

var t = setTimeout(function () { myTableObject.fnAdjustColumnSizing(false);}, 300);

This refreshes the table without making another unnecessary Ajax call, but it waits 300 ms before doing it, to 'let internet explorer do its thing' before readjusting. If you set lower values i.e. 10 ms) this doesn't work.

I hope it helps,

Roger

发布评论

评论列表(0)

  1. 暂无评论