javascript - Datatable, table overlapped when using fixed column - Stack Overflow

Base on the code I use below, my table is messed up when using fixed column. It seems that my table is

Base on the code I use below, my table is messed up when using fixed column. It seems that my table is overlapped at the first 3 left column. I suspect that scrollX is making the problem because the table looks perfectly fine without it.

Any ideas why this happen and how to solve it?

$('#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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</td>
	        </tr>
	    </tbody>
	</table>

Base on the code I use below, my table is messed up when using fixed column. It seems that my table is overlapped at the first 3 left column. I suspect that scrollX is making the problem because the table looks perfectly fine without it.

Any ideas why this happen and how to solve it?

$('#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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</td>
	        </tr>
	    </tbody>
	</table>

Share Improve this question edited Apr 9, 2019 at 5:43 Anandhukrishna VR 2792 silver badges17 bronze badges asked Apr 9, 2019 at 5:31 LearnProgrammingLearnProgramming 8061 gold badge13 silver badges42 bronze badges 1
  • Is the question about jQuery? You should tag it right... – The incredible Jan Commented Sep 27, 2022 at 13:22
Add a ment  | 

2 Answers 2

Reset to default 3

You should read dataTable documentation properly and include all css & js external script in your code

Your problem is missing some external script 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/fixedcolumns/3.2.6/js/dataTables.fixedColumns.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/datatables/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables/1.10.19/css/jquery.dataTables.min.csshttps://cdn.datatables/fixedcolumns/3.2.6/css/fixedColumns.dataTables.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare./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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</td>
        </tr>
    </tbody>
</table>

Include

dataTables.min.css

fixedColumns.dataTables.min.css

jquery.min.js

jquery.dataTables.min.js

dataTables.fixedColumns.min.js

I have fixed by removing table-checkable class

<table  class="table table-hover full-width" ></table>

table-checkable

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745342665a4623398.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信