Datatables on-the-fly resizing

后端 未结 12 2205
灰色年华
灰色年华 2020-12-12 16:21

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

相关标签:
12条回答
  • 2020-12-12 16:45

    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%');
        } );
    
    0 讨论(0)
  • 2020-12-12 16:46

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

    0 讨论(0)
  • 2020-12-12 16:50

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

    Example:

    $('#tableId').dataTable({
     "bAutoWidth": false
    });
    
    0 讨论(0)
  • 2020-12-12 16:51

    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.

    0 讨论(0)
  • 2020-12-12 16:53

    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.

    0 讨论(0)
  • 2020-12-12 16:55

    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

    0 讨论(0)
提交回复
热议问题