1

Sorry I am pretty new to Django and trying to laod a jQuery datatables with some data coming back from the server. the json coming back is in a good format. However, the data is not loading in the table and I get the following error in the firebug console:

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

Additionally, I tried to play around with the sAjaxDataProp option to adjust the default behavior of aaData but I couldn't figure what it should be set to. Anyways below is the code for everything

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 coming back from the url:

[
    {
        "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
        }
    }
]

I would appreciate it if someone could help.

1 Answer 1

2

The library is looking for the data to be in the property of aaData of the response from the server. Since the server is returning a list of objects, it's says it's undefined when it tries to access it.

You can't be using an empty string for sAjaxDataProp. You can read up more on what you should be returning from the server here: http://legacy.datatables.net/usage/server-side.

Change your jQuery part to:

$(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"},
        ]

    });
});

Change the response from the server to be:

{"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
        }
    }
]}

You should also be returning iTotalRecords, iTotalDisplayRecords, and sEcho in the response.

Sign up to request clarification or add additional context in comments.

1 Comment

This works for me. I'm using laravel though. I just added the data key. So, return json_encode(['data' => $users]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.