jQuery DataTables: control table width

后端 未结 26 2020
别那么骄傲
别那么骄傲 2020-11-29 16:29

I have a problem controlling the width of a table using the jQuery DataTables plugin. The table is supposed to be 100% of the container width, but ends up being an arbitrar

相关标签:
26条回答
  • 2020-11-29 17:23

    This is my way of doing it..

    $('#table_1').DataTable({
        processing: true,
        serverSide: true,
        ajax: 'customer/data',
        columns: [
            { data: 'id', name: 'id' , width: '50px', class: 'text-right' },
            { data: 'name', name: 'name' width: '50px', class: 'text-right' }
        ]
    });
    
    0 讨论(0)
  • 2020-11-29 17:26

    For anyone having trouble adjusting table / cell width using the fixed header plugin:

    Datatables relies on thead tags for column width parameters. This is because its really the only native html as most of the table's inner html gets auto-generated.

    However, what happens is some of your cell can be larger than the width stored inside the thead cells.

    I.e. if your table has a lot of columns (wide table) and your rows have a lot of data, then calling "sWidth": to change the td cell size won't work properly because the child rows are automatically resizing td's based on overflow content and this happens after the table's been initialized and passed its init params.

    The original thead "sWidth": parameters get overridden (shrunk) because datatables thinks your table still has its default width of %100--it doesn't recognize that some cells are overflowed.

    To fix this I figured out the overflow width and accounted for it by resizing the table accordingly after the table has been initialized--while we're at it we can init our fixed header at the same time:

    $(document).ready(function() {
      $('table').css('width', '120%');
      new FixedHeader(dTable, {
            "offsetTop": 40,
          });
    });
    
    0 讨论(0)
  • 2020-11-29 17:27
    jQuery('#querytableDatasets').dataTable({  
            "bAutoWidth": false
    });
    
    0 讨论(0)
  • 2020-11-29 17:28

    I Meet the same problem today.

    I used a tricky way to solve it.

    My Code as below.

    $('#tabs').tabs({
        activate: function (event, ui) {
           //redraw the table when the tab is clicked
           $('#tbl-Name').DataTable().draw();
        }
    });
    
    0 讨论(0)
  • 2020-11-29 17:29

    As on Datatable 10.1, this is straight forward. Just put width attribute to your table in HTML. i.e. width="100%", this will override all CSS styles set by DT.

    See this http://www.datatables.net/examples/basic_init/flexible_width.html

    0 讨论(0)
  • 2020-11-29 17:29

    This works fine.

    #report_container {
       float: right;
       position: relative;
       width: 100%;
    }
    
    0 讨论(0)
提交回复
热议问题