使用datatable首先需要下载datatable文件,文件主要包括三个文件夹css,img,js相关文件,在django中如何配置呢?

首先需要在模板中引入datatable文件,格式如下:

    <!-- DataTables CSS -->
    <link rel="stylesheet" href="{% static 'DataTables/css/jquery.dataTables.css' %}">
    <!-- jquery -->
    <script src="{% static 'js/jquery/jquery.min.js' %}"></script>
    <!-- DataTables -->
    <script src="{% static 'DataTables/js/jquery.dataTables.js' %}"></script>


注意datatables中的js文件需要引用在jquery文件之后.

那么django中如何让数据使用datatables表格呢?

代码如下:

     <table id="table_id" class="display">
            <thead>
                <tr>
                    <th>Column 1</th>
                    <th>Column 2</th>
                </tr>
            </thead>
            <tbody>
            {% for d in data %}
                <tr>
                    <td>{{ d.col1 }}</td>
                    <td>{{ d.col2 }}</td>
                </tr>
             {% endfor %}
            </tbody>
        </table>
<script>
$(document).ready( function () {
                $('#table_id').DataTable();
            } );
</script>


完毕!

相关文章:

  • 2022-12-23
  • 2021-05-06
  • 2021-08-13
  • 2022-02-04
  • 2021-08-09
  • 2021-07-28
  • 2021-10-15
  • 2021-07-30
猜你喜欢
  • 2022-12-23
  • 2021-07-23
  • 2021-05-29
  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案