javascript - Datatable、使用固定列时表格重叠

标签 javascript datatable

根据我下面使用的代码,使用固定列时我的表格变得困惑。看来我的表格在左前 3 列重叠。我怀疑是scrollX 造成了这个问题,因为没有它,表格看起来完全没有问题。

有什么想法为什么会发生这种情况以及如何解决吗?

enter image description here

$('#example').DataTable( {
    scrollY:        "300px",
    scrollX:        true,
    scrollCollapse: true,
    paging:         false,
    fixedColumns:   {
        leftColumns:3
    }
} );
<table id="example" class="table table-striped table-bordered table-hover table-checkable" style="width:100%">
	    <thead>
	        <tr>
	            <th>First name</th>
	            <th>Last name</th>
	            <th>Position</th>
	            <th>Office</th>
	            <th>Age</th>
	            <th>Start date</th>
	            <th>Salary</th>
	            <th>Extn.</th>
	            <th>E-mail</th>
	        </tr>
	    </thead>
	    <tbody>
	        <tr>
	            <td>Tiger</td>
	            <td>Nixon</td>
	            <td>System Architect</td>
	            <td>Edinburgh</td>
	            <td>61</td>
	            <td>2011/04/25</td>
	            <td>$320,800</td>
	            <td>5421</td>
	            <td>t.nixon@datatables.net</td>
	        </tr>
	        <tr>
	            <td>Garrett</td>
	            <td>Winters</td>
	            <td>Accountant</td>
	            <td>Tokyo</td>
	            <td>63</td>
	            <td>2011/07/25</td>
	            <td>$170,750</td>
	            <td>8422</td>
	            <td>g.winters@datatables.net</td>
	        </tr>
	        <tr>
	            <td>Ashton</td>
	            <td>Cox</td>
	            <td>Junior Technical Author</td>
	            <td>San Francisco</td>
	            <td>66</td>
	            <td>2009/01/12</td>
	            <td>$86,000</td>
	            <td>1562</td>
	            <td>a.cox@datatables.net</td>
	        </tr>
	        <tr>
	            <td>Cedric</td>
	            <td>Kelly</td>
	            <td>Senior Javascript Developer</td>
	            <td>Edinburgh</td>
	            <td>22</td>
	            <td>2012/03/29</td>
	            <td>$433,060</td>
	            <td>6224</td>
	            <td>c.kelly@datatables.net</td>
	        </tr>
	    </tbody>
	</table>

最佳答案

您应该正确阅读 dataTable 文档,并在代码中包含所有 css 和 js 外部脚本

您的问题是缺少一些外部脚本 Here is the working solution

$(document).ready(function() {
    var table = $('#example').DataTable( {
        scrollY:        "300px",
        scrollX:        true,
        scrollCollapse: true,
        paging:         false,
        fixedColumns:   {
            leftColumns: 2
        }
    } );
} );
/* Ensure that the demo table scrolls */
    th, td { white-space: nowrap; }
    div.dataTables_wrapper {
        width: 800px;
        margin: 0 auto;
    }
<script src="https://cdn.datatables.net/fixedcolumns/3.2.6/js/dataTables.fixedColumns.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.csshttps://cdn.datatables.net/fixedcolumns/3.2.6/css/fixedColumns.dataTables.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="example" class="stripe row-border order-column" style="width:100%">
    <thead>
        <tr>
            <th>First name</th>
            <th>Last name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
            <th>Extn.</th>
            <th>E-mail</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Tiger</td>
            <td>Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$320,800</td>
            <td>5421</td>
            <td>t.nixon@datatables.net</td>
        </tr>
        <tr>
            <td>Garrett</td>
            <td>Winters</td>
            <td>Accountant</td>
            <td>Tokyo</td>
            <td>63</td>
            <td>2011/07/25</td>
            <td>$170,750</td>
            <td>8422</td>
            <td>g.winters@datatables.net</td>
        </tr>
        <tr>
            <td>Ashton</td>
            <td>Cox</td>
            <td>Junior Technical Author</td>
            <td>San Francisco</td>
            <td>66</td>
            <td>2009/01/12</td>
            <td>$86,000</td>
            <td>1562</td>
            <td>a.cox@datatables.net</td>
        </tr>
        <tr>
            <td>Cedric</td>
            <td>Kelly</td>
            <td>Senior Javascript Developer</td>
            <td>Edinburgh</td>
            <td>22</td>
            <td>2012/03/29</td>
            <td>$433,060</td>
            <td>6224</td>
            <td>c.kelly@datatables.net</td>
        </tr>
    </tbody>
</table>

包含

dataTables.min.css

fixedColumns.dataTables.min.css

jquery.min.js

jquery.dataTables.min.js

dataTables.fixedColumns.min.js

关于javascript - Datatable、使用固定列时表格重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55585678/

相关文章:

javascript - 如何从应用程序脚本中使用 googleapis

python - 在 AWS 的 DynamoDB 表中设置列的顺序

c# - 在 DataSet 中使用函数时为 "undefined function call"

c# - 如何选择数据表中列的最小值和最大值?

javascript - 满足条件时计数错误

javascript - 从 HTML 表中检索列

javascript - 如何在样式元素中获取 CSS 代码?

javascript - 试图理解回调函数

c# - 如何检查 Linq 查询结果中的 DBNull 值

javascript - 在 asp.net 中的 GridView 或数据表中动态添加新行?