数据表,使用固定列时表重叠

时间:2019-04-09 05:31:14

标签: 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>

1 个答案:

答案 0 :(得分:0)

您应该正确阅读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