首页 > 解决方案 > 数据表固定表头

问题描述

我无法在数据表中制作固定标题

js

<script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/fixedheader/3.1.2/js/dataTables.fixedHeader.min.js" type="text/javascript"></script>

css

<link href="https://cdn.datatables.net/1.10.23/css/jquery.dataTables.min.css" rel="stylesheet" crossorigin="anonymous" />
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/fixedheader/3.1.8/css/fixedHeader.dataTables.min.css">

脚本

$(document).ready(function() {
    $('#dataTable').DataTable(
        {

            "paging": false,
            "fixedHeader": true,
            "bDestroy": true,
        }
    );
});

遗漏了什么 ?

{% load static %}

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <meta name="description" content="battery managament" />
        <meta name="author" content="mar24n" />
        <title>Dashboard - Battery</title>


        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.23/b-1.6.5/b-print-1.6.5/fh-3.1.8/datatables.min.css"/>
        <link href="{% static 'css/styles.css'%}" rel="stylesheet"/>

         <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/fixedheader/3.1.8/css/fixedHeader.dataTables.min.css">



        <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/js/all.min.js" crossorigin="anonymous"></script>
    </head>

    <body class="sb-nav-fixed">
        <div id="layoutSidenav">
            <div id="layoutSidenav_content">



                {% block content %}{% endblock %}
            </div>
        </div>
    </body>

</html>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.23/fh-3.1.8/datatables.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="{% static 'js/scripts.js' %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>
<script src="{% static 'assets/demo/chart-area-demo.js' %}"></script>
<script src="{% static 'assets/demo/chart-bar-demo.js' %}"></script>
<script src="https://cdn.datatables.net/fixedheader/3.1.2/js/dataTables.fixedHeader.min.js" type="text/javascript"></script>


<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js" crossorigin="anonymous"></script>

<script src="{% static 'assets/demo/datatables-demo.js' %}"></script>
<script src="{% static 'js/plugin.js' %}"></script>

<script>
const message_ele = document.getElementById("message_id");

setTimeout(function(){
message_ele.style.display = "none";
}, 3000);
</script>

<script>
$(document).ready(function() {
    $('#dataTable').DataTable(
        {

            "paging": false,
            "fixedHeader": true,
            "bDestroy": true,
        }
    );
});
</script>

表格内容

{% load static %}

{% block content %}

    {% include 'main/navbar.html' %}
    {% include 'main/sidenav.html' %}

<main>
    <div class="container-fluid">
        <h1 class="mt-4">Modules</h1>
        <ol class="breadcrumb mb-4">
            <div id="total_modules">
            {% include 'main/total_modules.html' %}
            </div>
        </ol>


        <div class="card mb-4">

            <div class="card-header">
                <div class="row">
                <button type="button" class="btn btn-success show-form">Add Module</button>
                    <a class="col-xl-1"></a>
                <button type="button" class="col-xl-1 btn btn-success btn-block show-test-form">New Test</button>
            </div>
        </div>
            <div class="card-body">
                <div>
                    <table id="dataTable" width="100%" class="display nowrap table-bordered table-striped" >
                        <thead>
                            <tr>
                                <th>Car</th>
                                <th>Serial Number</th>
                                <th>Date</th>
                                <th>Batch</th>
                                <th>Status</th>
                                <th>Last Test</th>
                                <th>Details</th>
                                <th>Added</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for module in modules_list %}
                                {% with latest_test=module.get_latest_test %}
                                <tr>
                                    <td>{{ module.car }}</td>
                                    <td>{{ module.serial_number }}</td>
                                    <td>{{ module.day }} {{ module.mon }} {{ module.year1 }}</td>
                                    <td>{{ module.batch }}</td>
                                    <td>{{ latest_test.cap|stringformat:".2f" }} %</td>
                                    <td>{{ latest_test.added_at|date:'d M Y H:i' }}</td>

                                    <td><a style="font-weight: bolder;" class="btn btn-secondary" href="{% url 'module_details' module.pk %}">Details</a> </td>
                                    <td>{{ module.added_at|date:'d M Y H:i' }}</td>
                                </tr>
                                {% endwith %}
                            {% endfor %}
                        </tbody>
                    </table>
                    <div>

                    </div>
                <div class="modal fade" id="modal-book" data-backdrop="static" data-keboard="false">
                    <div class="modal-dialog">
                        <div class="modal-content"></div>
                    </div>
                </div>
                    <div class="modal fade" id="modal-test" data-backdrop="static" data-keboard="false">
                    <div class="modal-dialog">
                        <div class="modal-content"></div>
                    </div>
                </div>
                </div>
            </div>
        </div>
    </div>
</main>


    {% include 'main/footer.html' %}

{% endblock %}

标签: javascriptdatatable

解决方案


在我看来,您的CDN链接是问题所在。按照以下示例更改链接。

要构建您自己的数据表并获取 cdn,请使用以下 url: https ://datatables.net/download/

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.23/fh-3.1.8/datatables.min.css"/>
 
<script type="text/javascript" src="https://cdn.datatables.net/v/dt/jq-3.3.1/dt-1.10.23/fh-3.1.8/datatables.min.js"></script>

<table id="example" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td>San Francisco</td>
                <td>66</td>
                <td>2009/01/12</td>
                <td>$86,000</td>
            </tr>
            <tr>
                <td>Cedric Kelly</td>
                <td>Senior Javascript Developer</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2012/03/29</td>
                <td>$433,060</td>
            </tr>
            <tr>
                <td>Airi Satou</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>33</td>
                <td>2008/11/28</td>
                <td>$162,700</td>
            </tr>
            <tr>
                <td>Brielle Williamson</td>
                <td>Integration Specialist</td>
                <td>New York</td>
                <td>61</td>
                <td>2012/12/02</td>
                <td>$372,000</td>
            </tr>
            <tr>
                <td>Herrod Chandler</td>
                <td>Sales Assistant</td>
                <td>San Francisco</td>
                <td>59</td>
                <td>2012/08/06</td>
                <td>$137,500</td>
            </tr>
            <tr>
                <td>Rhona Davidson</td>
                <td>Integration Specialist</td>
                <td>Tokyo</td>
                <td>55</td>
                <td>2010/10/14</td>
                <td>$327,900</td>
            </tr>
            <tr>
                <td>Colleen Hurst</td>
                <td>Javascript Developer</td>
                <td>San Francisco</td>
                <td>39</td>
                <td>2009/09/15</td>
                <td>$205,500</td>
            </tr>
            <tr>
                <td>Sonya Frost</td>
                <td>Software Engineer</td>
                <td>Edinburgh</td>
                <td>23</td>
                <td>2008/12/13</td>
                <td>$103,600</td>
            </tr>
            <tr>
                <td>Jena Gaines</td>
                <td>Office Manager</td>
                <td>London</td>
                <td>30</td>
                <td>2008/12/19</td>
                <td>$90,560</td>
            </tr>
            <tr>
                <td>Quinn Flynn</td>
                <td>Support Lead</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2013/03/03</td>
                <td>$342,000</td>
            </tr>
            <tr>
                <td>Charde Marshall</td>
                <td>Regional Director</td>
                <td>San Francisco</td>
                <td>36</td>
                <td>2008/10/16</td>
                <td>$470,600</td>
            </tr>
            <tr>
                <td>Haley Kennedy</td>
                <td>Senior Marketing Designer</td>
                <td>London</td>
                <td>43</td>
                <td>2012/12/18</td>
                <td>$313,500</td>
            </tr>
            <tr>
                <td>Tatyana Fitzpatrick</td>
                <td>Regional Director</td>
                <td>London</td>
                <td>19</td>
                <td>2010/03/17</td>
                <td>$385,750</td>
            </tr>
            <tr>
                <td>Michael Silva</td>
                <td>Marketing Designer</td>
                <td>London</td>
                <td>66</td>
                <td>2012/11/27</td>
                <td>$198,500</td>
            </tr>
            <tr>
                <td>Paul Byrd</td>
                <td>Chief Financial Officer (CFO)</td>
                <td>New York</td>
                <td>64</td>
                <td>2010/06/09</td>
                <td>$725,000</td>
            </tr>
            <tr>
                <td>Gloria Little</td>
                <td>Systems Administrator</td>
                <td>New York</td>
                <td>59</td>
                <td>2009/04/10</td>
                <td>$237,500</td>
            </tr>
            <tr>
                <td>Bradley Greer</td>
                <td>Software Engineer</td>
                <td>London</td>
                <td>41</td>
                <td>2012/10/13</td>
                <td>$132,000</td>
            </tr>
        </tbody>
</table>

<script>
    $('#example').DataTable( {
        "paging": false,
        "fixedHeader": true,
        "bDestroy": true
    });
</script>
            
         

希望这可以帮助


推荐阅读