Column width recalculation
Column width recalculation
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.
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.
This discussion has been closed.
Replies
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
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]
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