Django - jQuery数据表停留在加载上

时间:2014-06-03 17:39:16

标签: jquery json django datatables

对不起,我对Django很陌生,并试图用一些从服务器返回的数据来处理jQuery数据表。 json回来的格式很好。但是,数据未加载到表中,我在firebug控制台中收到以下错误:

TypeError: aData is undefined
for ( i=0 ; i<aData.length ; i++ ) {

此外,我尝试使用sAjaxDataProp选项来调整aaData的默认行为,但我无法确定它应该设置为什么。无论如何,下面是一切的代码

jquery的:

$(document).ready(function () {
    $('#rfctable').dataTable({
        "sAjaxDataProp": '', // I don't know if I need this or how to deal with it
        "ajax": 'http://127.0.0.1:8000/api/',
        "columns": [
            { "fields": "rfc_number"},
            { "fields": "rfc_title"},
            { "fields": "rfc_question"},
        ]

    });
});

HTML:

<table id="rfctable" class="display" cellspacing="0" width="100%">
    <thead>
    <tr>
        <th>Rfc Number</th>
        <th>RFC title</th>
        <th>RFC Questions</th>
    </tr>
    </thead>
</table>

json从网址回来:

[
    {
        "pk": 1,
        "model": "rfc.rfcdocument",
        "fields": {
            "rfc_title": "123123123123",
            "rfc_answer_reviewed_by": 1,
            "rfc_required_fcd": true,
            "rfc_drawing_detail_number": "123",
            "rfc_required_sketch": true,
            "rfc_answer_authorized_by": 1,
            "rfc_issued_by": 1,
            "rfc_answer_issued_date": null,
            "rfc_specification_section": "34-5",
            "rfc_answered_date_architect": null,
            "rfc_question": "Salam baba?",
            "rfc_issued_date": null,
            "rfc_answer": "salama back!",
            "rfc_project": 1,
            "rfc_required_fls_review": true,
            "rfc_drawing_page_number": "54",
            "rfc_issued_to": 1
        }
    }
]

如果有人可以提供帮助,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

库正在查找数据位于服务器响应的aaData属性中。由于服务器返回一个对象列表,因此在尝试访问它时它是未定义的。

您不能为sAjaxDataProp使用空字符串。您可以在此处详细了解您应该从服务器返回的内容:http://legacy.datatables.net/usage/server-side

将您的jQuery部分更改为:

$(document).ready(function () {
    $('#rfctable').dataTable({
        "sAjaxDataProp": 'data',
        "ajax": 'http://127.0.0.1:8000/api/',
        "columns": [
            { "fields": "rfc_number"},
            { "fields": "rfc_title"},
            { "fields": "rfc_question"},
        ]

    });
});

将服务器的响应更改为:

{"data": [
    {
        "pk": 1,
        "model": "rfc.rfcdocument",
        "fields": {
            "rfc_title": "123123123123",
            "rfc_answer_reviewed_by": 1,
            "rfc_required_fcd": true,
            "rfc_drawing_detail_number": "123",
            "rfc_required_sketch": true,
            "rfc_answer_authorized_by": 1,
            "rfc_issued_by": 1,
            "rfc_answer_issued_date": null,
            "rfc_specification_section": "34-5",
            "rfc_answered_date_architect": null,
            "rfc_question": "Salam baba?",
            "rfc_issued_date": null,
            "rfc_answer": "salama back!",
            "rfc_project": 1,
            "rfc_required_fls_review": true,
            "rfc_drawing_page_number": "54",
            "rfc_issued_to": 1
        }
    }
]}

您还应该在回复中返回iTotalRecordsiTotalDisplayRecordssEcho