1

I'm just trying an example of datatables with query with the following json object...

[{"firstName":"pom",
"lastName":"sdfpom",
"email":null,
"password":"cfe9a43acec0a35f903bc2d646ce8e58b5ef6b67",
"username":"Dave",
 "access":null,
"id":1},
{"firstName":"FirstName",
"lastName":"LastName",
"email":null,
"password":"8d60258ef3ae1b8eae67e9298f15edf5c6e05dfe",
"username":"Username",
"access":null,
"id":2}]

This is returned in the variable data below...

<script>
$(document).ready(function() {
    $.getJSON('userManagement/getAllUsers', function(data) {
            $('#table').dataTable( {
                "sAjaxSource": data
        });
    });
});

    </script>
    <table id="table">
        <thead>
            <tr>
                <th>firstName</th>
                <th>lastName</th>
                <th>email</th>
                <th>password</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Row 1 Data 1</td>
                <td>Row 1 Data 2</td>
                <td>etc</td>
            </tr>
            <tr>
                <td>Row 2 Data 1</td>
                <td>Row 2 Data 2</td>
                <td>etc</td>
            </tr>
        </tbody>
    </table>

Now the JSON seems to be valid, and when I do anything else with it for instance use it within jquery it works fine, but datatables just doesn't render correctly at all. Is there something wrong with the javascript I'm using?

4 Answers 4

5

By default, DataTables will process an array of arrays for its data source: there's an additional step when it has to deal with array of objects (as in your case). It's described in this example in the plugin documentation. Basically, what you have to do is to add description of 'column' properties (as array):

$('#table').dataTable({
  "aaData": data,
  "aoColumns": [
    { "mData": "firstName" },
    { "mData": "lastName" },
    { "mData": "email" },
    { "mData": "password" },
    { "mData": "username" },
    { "mData": "access" },
    { "mData": "id" }
  ]
});

Here's fiddle to play with.

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

3 Comments

This does now show the table, but it's empty other than the <td> rows I've added in the html, when I remove these the JSON data does not populate the table. Do you have any other ideas? Thank you for your feedback :)
Updated the answer: as you work with JS structure, you need to use 'aaData' key; sAjaxSource is used to give an URL that returns the source JSON.
thanks to your fiddle I managed to work out to return data = JSON.Parse(result.d); return data; This is for new dataTables, which doesn't use aoColumns any more, but still, worked it out after a few hours of messing about!
1

Your json is an object inside an array..

So instead of this

"sAjaxSource": data

Try this

"sAjaxSource": data[0]

1 Comment

This just shows the <tbody> data in the html - when I remove the tBody data altogether it just shows the header. none of my JSON data is displayed. Thank you for your feedback though :)
1
$.ajax( {
          "dataType": 'json',
          "type": "POST",
          "url": sSource,
          "data": aoData,
          contentType: "application/json; charset=utf-8",
          async : false,
          success: function (json) { 
            fnCallback(json);
          },
          complete: function (msg,a,b) {
            console.log('complete :'+msg) 
          },
          error : function(msg,a,b) {
            console.log('error:'+msg);
          }
        } );

The async : false is very important. This will cause not to render the page until the json data is bounded properly.

This worked for me.

Comments

0

first of all check if your json is valid or not use www.jsonlint.com for the purpose.

Secondly Try to enclose your JSON object with aaData like:

{"aaData" :[{"firstName":"pom","lastName":"sdfpom","email":null,
"password":"cfe9a43acec0a35f903bc2d646ce8e58b5ef6b67",
"username":"Dave","access":null,"id":1},
{"firstName":"FirstName","lastName":"LastName","email":null,
"password":"8d60258ef3ae1b8eae67e9298f15edf5c6e05dfe",
"username":"Username","access":null,"id":2}] 
}

3 Comments

Yes the JSON is valid, I can't add to the JSON as it's being generated from Jackson with SpringMVC so the transition from object to JSON representation is seamless.
Listen if the json generated is the same as the one given by you above then it generates an error.
That's just the line rendering of the stackoverflow code tag, I'll format it but it does generate valid JSON

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.