Datatables on-the-fly resizing

JavascriptJqueryDatatables

Javascript Problem Overview


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! :-)

Javascript Solutions


Solution 1 - Javascript

What is happening is that DataTables is setting the CSS width of the table when it is initialised to a calculated value - that value is in pixels, hence why it won't resize with your dragging. The reason it does this is to stop the table and the columns (the column widths are also set) jumping around in width when you change pagination.

What you can do to stop this behaviour in DataTables is set the autoWidth parameter to false.

$('#example').dataTable( {
  "autoWidth": false
} );

That will stop DataTables adding its calculated widths to the table, leaving your (presumably) width:100% alone and allowing it to resize. Adding a relative width to the columns would also help stop the columns bouncing.

One other option that is built into DataTables is to set the sScrollX option to enable scrolling, as DataTables will set the table to be 100% width of the scrolling container. But you might not want scrolling.

The prefect solution would be if I could get the CSS width of the table (assuming one is applied - i.e. 100%), but without parsing the stylesheets, I don't see a way of doing that (i.e. basically I want $().css('width') to return the value from the stylesheet, not the pixel calculated value).

Solution 2 - Javascript

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

Solution 3 - Javascript

This did the trick for me.

$('#dataTable').resize()

Solution 4 - Javascript

$(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,

Solution 5 - Javascript

You should try this one.

var table = $('#example').DataTable();
table.columns.adjust().draw();

Link: column adjust in datatable

Solution 6 - Javascript

Use "bAutoWidth": false and go through the example given below. It is working for me.

Example:

$('#tableId').dataTable({
 "bAutoWidth": false
});

Solution 7 - Javascript

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%');
	} );

Solution 8 - Javascript

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, ...});

Solution 9 - Javascript

Have you tried capturing the div resize event and doing .fnDraw() on the datatable? fnDraw should resize the table for you

Solution 10 - Javascript

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.)

Solution 11 - Javascript

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.

Solution 12 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEmphram StavangerView Question on Stackoverflow
Solution 1 - JavascriptAllan JardineView Answer on Stackoverflow
Solution 2 - JavascriptStephenView Answer on Stackoverflow
Solution 3 - JavascriptjpsView Answer on Stackoverflow
Solution 4 - JavascriptRui MartinsView Answer on Stackoverflow
Solution 5 - JavascriptChintan PanchalView Answer on Stackoverflow
Solution 6 - Javascriptuser2314493View Answer on Stackoverflow
Solution 7 - JavascriptmezzieView Answer on Stackoverflow
Solution 8 - JavascriptEmmanuel Robert SsebaggalaView Answer on Stackoverflow
Solution 9 - JavascriptKeith.AbramoView Answer on Stackoverflow
Solution 10 - JavascriptDave SagView Answer on Stackoverflow
Solution 11 - JavascriptAlberto HormazabalView Answer on Stackoverflow
Solution 12 - JavascriptStackOverflowUserView Answer on Stackoverflow