[django]django+datatable簡單運用於表格中


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


完畢!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM