jQuery DataTables: control table width

后端 未结 26 2045
别那么骄傲
别那么骄傲 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:04

    You'll want to tweak two variables when you initialize dataTables: bAutoWidth and aoColumns.sWidth

    Assuming you have 4 columns with widths of 50px, 100, 120px and 30px you would do:

    jQuery('#querytableDatasets').dataTable({  
            "bPaginate": false,  
            "bInfo": false,  
            "bFilter": false,
            "bAutoWidth": false,
            "aoColumns" : [
                { sWidth: '50px' },
                { sWidth: '100px' },
                { sWidth: '120px' },
                { sWidth: '30px' }
            ]  
        }); 
    

    More information on the initialization of dataTables can be found at http://datatables.net/usage

    Watch for interaction between this setting of widhts and the CSS you are applying. You might comment your existing CSS before trying this to see how close you get.

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

    i had the similar problem and found that text in columns don't break and using this css code solved the problem

    th,td{
    max-width:120px !important;
    word-wrap: break-word
    }
    
    0 讨论(0)
  • 2020-11-29 17:11

    The issue is caused because dataTable must calculate its width - but when used inside a tab, it's not visible, hence can't calculate the widths. The solution is to call 'fnAdjustColumnSizing' when the tab shows.

    Preamble

    This example shows how DataTables with scrolling can be used together with jQuery UI tabs (or indeed any other method whereby the table is in a hidden (display:none) element when it is initialised). The reason this requires special consideration, is that when DataTables is initialised and it is in a hidden element, the browser doesn't have any measurements with which to give DataTables, and this will require in the misalignment of columns when scrolling is enabled.

    The method to get around this is to call the fnAdjustColumnSizing API function. This function will calculate the column widths that are needed based on the current data and then redraw the table - which is exactly what is needed when the table becomes visible for the first time. For this we use the 'show' method provided by jQuery UI tables. We check to see if the DataTable has been created or not (note the extra selector for 'div.dataTables_scrollBody', this is added when the DataTable is initialised). If the table has been initialised, we re-size it. An optimisation could be added to re-size only of the first showing of the table.

    Initialisation code

    $(document).ready(function() {
        $("#tabs").tabs( {
            "show": function(event, ui) {
                var oTable = $('div.dataTables_scrollBody>table.display', ui.panel).dataTable();
                if ( oTable.length > 0 ) {
                    oTable.fnAdjustColumnSizing();
                }
            }
        } );
    
        $('table.display').dataTable( {
            "sScrollY": "200px",
            "bScrollCollapse": true,
            "bPaginate": false,
            "bJQueryUI": true,
            "aoColumnDefs": [
                { "sWidth": "10%", "aTargets": [ -1 ] }
            ]
        } );
    } );
    

    See this for more info.

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

    Just to say I've had exactly the same problem as you, although I was just apply JQuery to a normal table without any Ajax. For some reason Firefox doesn't expand the table out after revealing it. I fixed the problem by putting the table inside a DIV, and applying the effects to the DIV instead.

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

    Are you doing this in the ready event?

    $(document).ready(function() { ... });
    

    The sizing is most likely going to be dependent on the document being fully loaded.

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

    Be sure that all others parameters before bAutoWidth & aoColumns are correctly entered.Any wrong parameter before will break this functionality.

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