Column width recalculation

Column width recalculation

gpgp Posts: 2Questions: 0Answers: 0
edited September 2009 in General
Hello,

First of all, thanks a lot for DataTables -- it's amazing!

I've got one question: how can I force DataTables to recalculate column widths? I use table with ajax data source, so it is empty when page is loaded; but when ajax data is fetched, table starts to look ugly, because precalculated width values are not updated.

I've tried calling _fnCalculateColumnWidths + _fnInitalise, but with no success.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi gp,

    There isn't really a function for this at the moment. What would probably be needed is a plug-in API function which will run _fnCalculateColumnWidths and then apply the calculated widths (stored in oSettings.aoColumns[iIndex].sWidth) to the required TH elements.

    If you give it a shot, would you mind posting the code here, so I could add it to the Plug-ins page? It might be most useful for others as well.

    Regards,
    Allan
  • gpgp Posts: 2Questions: 0Answers: 0
    Thanks for the answer!

    I am really bad at JavaScript, and I don't think the code below is the right one... but here it is:

    [code]
    var update_auto_width = function (t) {
    var s = t.fnSettings();
    t.oApi._fnCalculateColumnWidths(s);
    t.oApi._fnDrawHead(s);
    t.oApi._fnDraw(s);
    }
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi,

    I think that the basic idea is more or less correct (see this page for how to plumb your function to the DataTables API: http://datatables.net/development/api ), but I don't think these two are correct.

    [code]
    t.oApi._fnDrawHead(s);
    t.oApi._fnDraw(s);
    [/code]

    Firstly, there is no need for the second line - there is nothing to redraw (i.e. no new rows are being displayed), so that can be dropped. Secondly - _fnDrawHead() I doubt that it will work with this being called more than once on a DataTables object. What you'll need to do is not use that function, and instead loop over aoColumns and apply the calculated with to the individual columns.

    Regards,
    Allan
  • martin@sommer.netmartin@sommer.net Posts: 15Questions: 0Answers: 0
    Was the code ever completed for this, and if so, can you please share it?
This discussion has been closed.