数据表标题在页面加载时不是全宽

Tim*_*m W 5 javascript header modal-dialog datatables

使用数据表加载我的Web应用程序页面时,会加载数据表,但该<thead>行不是表的全宽.

编辑数据或在数据表搜索中搜索后,标题跳转到全宽.见图.

有什么解决方案或解决方法吗?

数据表截图:

数据表的屏幕截图

我的常规数据表很好,但是这个数据表在一个模态中加载.代码模式:

<!-- Get selected shops modal -->
<div id="SelectedShopsModal" class="infomodal">
    <div class="shops-content">
        <h2><span class="closeModalSelectedShops">&times;</span> <?php echo lang('Shops'); ?></h2>
        <div class="detailblock">
            <table id="DataListTableSelectedShops" class="display" cellspacing="0" width="100%">
                <thead>
                    <th class="OE"><?php echo lang('Shop_No'); ?></th>
                    <th class="OE"><?php echo lang('Shop_Name'); ?></th>
                    <th class="OE"><?php echo lang('Shop_District'); ?></th>
                </thead>
                <tbody>
                    <?php foreach($selected_shops as $shop){
                        echo "<tr><td>" . $shop['Shop'] . "</td><td>" . $shop['Name'] . "</td><td>" . $shop['District'] . "</td></tr>";
                    } ?>
                </tbody>
            </table>
            <br /><button class="closeBtnSelectedShops btn red"><?php echo lang('Close'); ?></button>
            <button class="btn red" onclick="delete_oneshots();"><i class="fa fa-trash" aria-hidden="true"></i> <?php echo lang('Delete_shops'); ?></button>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我在页面页脚中使用的javascript:

//datatable to display all selected shops on detail page
selectedshoptable = $('#DataListTableSelectedShops').DataTable( {
    "dom": "Bfrtip",
    "scrollY": "300px",
    "scrollCollapse": true,
    "bPaginate": false,
    "bInfo" : false,
    "fields": [ {
        label: "Shopnr",
        name: "Shop"
    }, {
        label: "Shopname:",
        name: "Name"
    }, {
        label: "District",
        name: "District"
    }],
    "buttons":[{
        extend: 'selectAll',
        text: '<?php echo lang('Select_all'); ?>',
        className: 'btn red'
    }, {
        text: '<?php echo lang('Select_none'); ?>',
        className: 'btn red',
        action: function () {
            selectedshoptable.rows().deselect();
        }
    }],
    "language": {
        "zeroRecords": "<?php echo lang('Nothing_found'); ?>",
        "infoEmpty": "<?php echo lang('No_records_available'); ?>",
        "search":"<?php echo lang('Search'); ?>",
        "buttons": {"selectNone": "Select none"}
    }
});
Run Code Online (Sandbox Code Playgroud)

Gyr*_*com 6

原因

您的表最初是隐藏的,这会阻止jQuery DataTable调整列宽.

您需要在显示表时处理事件并调用columns().adjust()API方法.这将在显示更改后重新计算列宽.

$('#myCollapsible').on('shown.bs.collapse', function () {
   $($.fn.dataTable.tables(true)).DataTable()
      .columns.adjust();
});
Run Code Online (Sandbox Code Playgroud)

有关responsive.recalc()更多信息,请参阅API方法.

链接

请参阅jQuery DataTables - Bootstrap选项卡的列宽问题,以解决最初隐藏表时jQuery DataTables中列的最常见问题.

  • 感谢您的回答.我添加了selectedshoptable.columns.adjust(); 到我的模态的JS函数,它工作正常. (2认同)