1

I am trying to render a data table by making an AJAX call getting some data from controller and then writing data table. sData['id'] is just a number

Here is my code:

$.post('/admin/user_groups_data/' + sData['id']).done(function(data) {

        $('#user_groups_table').dataTable({
        "bProcessing": true,
        "bDeferRender": true,
        "sPaginationType": "full_numbers",
        "aaData": data, // data here is a JSON object, shows on Firebug correct data and fields

        "aoColumns": [
            { "mData": "id"},
            { "mData": "title" },
            { "mData": "category" },

        ]
    });

  });

Below is my HTML code

<table id="user_groups_table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Type</th>
                    </tr>
                </thead>

        </table>

It seems like the data table gets rendered first even before AJAX call is done so it gives me error

DataTables warning (table id = 'user_groups_table'): Requested unknown parameter 'id' from the data source for row 0

I have the .done at top but seems like it doesn't even respect that. Any help would be great. thx

7
  • The data object has the same key "id"? Commented Mar 7, 2014 at 23:07
  • sData['id'] comes from an object, while "mData" "id" comes from a AJAX call. That isn't the issue. Commented Mar 7, 2014 at 23:09
  • I know but mData:"id" tells datatable to find a property "id" on the data returned on the AJAX call that's what i'm referring to Commented Mar 7, 2014 at 23:11
  • If you data object is not like [{'id':1, .. ... .. }] maybe that's the error Commented Mar 7, 2014 at 23:17
  • Have you scanned the value "data" via console.log(data) to see if you are getting the right data? Commented Mar 7, 2014 at 23:24

1 Answer 1

1

Just added sAjaxSource to it and it works now, seems like the $.post had not completed yet when data table was being written, sAjaxSource retrieves the data and brings it back and then writes data table.

var oTable = $('#user_groups_table').dataTable({
        "bDestroy": true,
        "bProcessing": true,
        "bDeferRender": true,
        "sPaginationType": "full_numbers",
        //"aaData": data,
        "sAjaxSource": '/admin/user_groups_data/' + sData['id'],
        "aoColumns": [
            { "mData": "group_id", "bVisible": false},
            { "mData": "title" },
            { "mData": "category" },

        ]
        });
Sign up to request clarification or add additional context in comments.

Comments

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.