数据表scrollX水平不起作用

NHH*_*NHH 3 css jquery datatables

我有以下表格,其中包含 10 多列。我正在使用数据表来显示数据。我使用水平滚动,因为我有很多列。但卷轴没有显示出来。请帮忙。这是我的桌子的屏幕截图

滚动不起作用

<table id="example" class="display nowrap" cellspacing="0" width="100%">
    <thead>
        <tr>

    <th><center>Outlet ID</center></th>
    <th><center>Outlet Name</center></th>
    <th><center>Date</center></th>
    <th><center>Day</center></th>
    <th><center>Day Part</center></th>
    <th><center>Service</center></th>
    <th><center>Product</center></th>
    <th><center>Staff</center></th>
    <th><center>Pest</center></th>
    <th><center>Others</center></th>
    <th><center>Attentiveness</center></th>
    <th><center>Accuracy</center></th>
    <th><center>Speed</center></th>
    <th><center>Friendliness</center></th>
    <th><center>Food Handling & Hygiene</center></th>
    <th><center>Attentiveness</center></th>
    <th><center>Accuracy</center></th>
    <th><center>Speed</center></th>
    <th><center>Food</center></th>
    <th><center>Manpower</center></th>
    <th><center>Faulty Equipment</center></th>
    <th><center>Staff Behavior</center></th>
    <th><center>Restaurant Cleanliness</center></th>
    <th colspan=3><center>Action</center></th>
    </tr>
    </thead>
    <tbody>


     #code continue...

    </tbody>
    </table>
Run Code Online (Sandbox Code Playgroud)

这是我的 css 和滚动脚本

<style>
div.dataTables_wrapper {
    width: 800px;
    margin: 0 auto;
}
</style>


<script>
$(document).ready(function() {
$('#example').DataTable( {
    "scrollY": 300,
    "scrollX": true
} );
} );
</script>
Run Code Online (Sandbox Code Playgroud)

我需要显示水平滚动,因为我有超过 10 列。请帮忙。结果应该是这样的

Hüs*_*dag 6

制作网格responsible并删除withCSS 样式。

也可以指定列宽 [ width : "10%" or width:"100px"]

<table id="example" class="display nowrap" cellspacing="0" width="100%">

<table id="example" class="display nowrap" cellspacing="0">

分享我的代码示例

CSHTML

<div class="row">
<div class="col-12">
    <div class="card">
        <div class="card-header">
            <h4>Customers</h4>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table  table-striped table-hover" id="tblCustomers">
                    <thead>
                        <tr>
                            <th>CustomerId</th>     
                            <th>CustomerName</th>                          
                            <th>#</th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

JS文件

   $('#tblCustomers').dataTable({
            "scrollX": true,        
            // other Options
            "columns": [
                { "data": "CustomerId", "title": "Customer Id", "width": "30px", "bSortable": false },
                { "data": "CustomerName", "title": "Name", "width": "250px", "bSortable": false }
            ],
            "columnDefs": [
                {
                    "render": function (data, type, row) {
                        return "<a data-customerid='" + row.CustomerId+ "' class='btn btn-primary customer-detail' href='#'>Detail</a>"
                    },
                    "targets": [2],
                    "class": "text-center",
                    "width": '70px'
                }
            ]
        });
    }
Run Code Online (Sandbox Code Playgroud)