I'm using the marvellous DataTables jQuery plug-in; http://datatables.net/ Added the FixedColumns and KeyTable extras.
Now the table does resize prettily when the window size is changed. However, the containing div of the table can also be resized in width by a jQuery animation, and I haven't found a way to resize the table with it-- it just stays stagnant in its original width. Only if I change the div width in the code before pageload, the table is resized correctly.
How can I make the DataTable resize on-the-fly according to both the window width and the containing div width? Thanks in advance! :-)
This question is related to
javascript
jquery
datatables
This did the trick for me.
$('#dataTable').resize()
I got this to work as follows:
First ensure that in your dataTable
definition your aoColumns
array includes sWidth
data expressed as a % not fixed pixels or ems.
Then ensure you have set the bAutoWidth
property to false
Then add this little but of JS:
update_table_size = function(a_datatable) {
if (a_datatable == null) return;
var dtb;
if (typeof a_datatable === 'string') dtb = $(a_datatable)
else dtb = a_datatable;
if (dtb == null) return;
dtb.css('width', dtb.parent().width());
dtb.fnAdjustColumSizing();
}
$(window).resize(function() {
setTimeout(function(){update_table_size(some_table_selector_or_table_ref)}, 250);
});
Works a treat and my table cells handle the white-space: wrap;
CSS (which wasn't working without setting the sWidth
, and was what led me to this question.)
might be late also like the other answer but I did this early this year and the solution I came up with is using css.
$(window).bind('resize', function () {
/*the line below was causing the page to keep loading.
$('#tableData').dataTable().fnAdjustColumnSizing();
Below is a workaround. The above should automatically work.*/
$('#tableData').css('width', '100%');
} );
I know this is old, but I just solved it with this:
var update_size = function() {
$(oTable).css({ width: $(oTable).parent().width() });
oTable.fnAdjustColumnSizing();
}
$(window).resize(function() {
clearTimeout(window.refresh_size);
window.refresh_size = setTimeout(function() { update_size(); }, 250);
});
Note: This answer applies to DataTables 1.9
The code below is the combination of Chintan Panchal's answer along with Antoine Leclair's comment (placing the code in the windows resize event). (I didn't need the debounce mentioned by Antoine Leclair, however that could be a best practice.)
$(window).resize( function() {
$("#example").DataTable().columns.adjust().draw();
});
This was the approach that worked in my case.
Use "bAutoWidth": false
and go through the example given below. It is working for me.
Example:
$('#tableId').dataTable({
"bAutoWidth": false
});
I had the same challenge. When I collapsed some menus I had on the left of my web app, the datatable would not resize. Adding "autoWidth": false duirng initialization worked for me.
$('#dataTable').DataTable({'autoWidth':false, ...});
You should try this one.
var table = $('#example').DataTable();
table.columns.adjust().draw();
$(document).ready(function() {
$('a[data-toggle="tab"]').on( 'shown.bs.tab', function (e) {
// var target = $(e.target).attr("href"); // activated tab
// alert (target);
$($.fn.dataTable.tables( true ) ).css('width', '100%');
$($.fn.dataTable.tables( true ) ).DataTable().columns.adjust().draw();
} );
});
It works for me, with "autoWidth": false,
Have you tried capturing the div resize event and doing .fnDraw()
on the datatable? fnDraw
should resize the table for you
I was having the exact same problem as OP. I had a DataTable which would not readjust its width after a jQuery animation (toogle("fast")) resized its container.
After reading these answers, and lots of try and error this did the trick for me:
$("#animatedElement").toggle(100, function() {
$("#dataTableId").resize();
});
After many test, i realized that i need to wait for the animation to finish for dataTables to calculate the correct width.
Source: Stackoverflow.com