Jquery数据表Ajax分页

时间:2016-09-29 17:34:13

标签: jquery ajax datatables

你有这个JS脚本:

<script type="text/javascript" src="/bower_components/datatables.net/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
<script>
    $(document).ready(function() {
        $('#usersTable').DataTable({
            "sPaginationType": "full_numbers",
            "bProcessing": true,
            "bServerSide": true,
            "bDeferRender": true,
            "sAjaxSource": "{{ path('prospection_prospects_ajax') }}",
            "bJQueryUI": true,
            "sPaginationType": "full_numbers"
        });
        $.fn.dataTableExt.sErrMode = 'throw';
    });
</script>

此HTML代码:

<table id="usersTable" class="table">
    <thead>
    <tr>
        <th>Societe</th>
        <th style="width:150px;">Telephone</th>
        <th>Description</th>
        <th>Actions</th>
    </tr>
    </thead>
    <tbody>
    {% for prospect in prospects %}
        <tr>
            <td>{{ prospect.societe}}</td>
            <td>{{ prospect.telephone }}</td>
            <td>{{ prospect.description }}</td>
            <td>
                <a href="{{ path("prospection_prospect_details",{userId:user.id}) }}">Details</a><br />
                <a href="{{ path("prospection_prospect_details",{userId:user.id}) }}">Supprimer</a><br />
                <select>
                    <option value="prospect">Prospect</option>
                    <option value="Contacter">Contacté</option>
                </select>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>

我将返回此JSON(我将100条记录返回100条记录):

{
"recordsTotal": 5055,
"recordsFiltered": 5055,
"draw": 1,
"records": [{
    "name": "Alerys",
    "telephone": "01 42 96 08 37",
    "description": "Prestations : cabinet de recrutement informatique, Evaluation, « Assessment Center », Bilan de carrière, Conseils en mobilité professionnelle et en organisation, Audit, Délégation de ressources, chasseur de tête, test psychotechnique, reconversion profess",
    "link": "<select><option value='prospect'>Prospect</option><option value='Contacter'>Contacté</option></select>"
}, {
    "name": "EuroWin Consulting Group",
    "telephone": "01 42 27 73 00",
    "description": "cabinet de recrutement informatique",
    "link": "<select><option value='prospect'>Prospect</option><option value='Contacter'>Contacté</option></select>"
} ...
]

Datable抛出了这个错误:Uncaught Error:DataTables警告:table id = usersTable - JSON响应无效。

我无法进行作品分页。

有人可以帮助我吗?

祝你好运

1 个答案:

答案 0 :(得分:0)

您确定records是密钥的名称。

在Datatable的官方网站上,密钥的名称为data

  

https://github.com/firebase/friendlychat/blob/master/android/app/src/main/java/com/google/firebase/codelab/friendlychat/MyFirebaseMessagingService.java