[django]django+datatable简单运用于表格中

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

首先须要在模板中引入datatable文件,格式以下:jquery

    <!-- 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

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

代码以下:code

     <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>


完毕!blog

相关文章
相关标签/搜索