0

I'm trying to populate a table with a JSON file using DataTables, but every time I load up the page, the table just shows "No data available in table".

This is my current code:

<table id="table_id" class="display">
<thead>
    <tr>
        <th>ID</th>
        <th>Nombre</th>
        <th>Apellido</th>
        <th>Mail</th>
        <th>Confirmado</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Row 1 Data 1</td>
        <td>Row 1 Data 2</td>
    </tr>
    <tr>
        <td>Row 2 Data 1</td>
        <td>Row 2 Data 2</td>
    </tr>
</tbody>
</table>

<script>
    $(document).ready(function () {
        $('#table_id').DataTable({
            "ajax" : {"url":"/personas.json", "dataSrc":""},
            "columns" : [
                {personas : "id"},
                {personas : "nombre"},
                {personas : "apellido"},
                {personas : "email"},
                {personas : "confirmado"}
            ]
        });
    });
</script>

This is a piece of the JSON code:

{
    "personas": [
        {
            "id": 0,
            "nombre": "Aurelia",
            "apellido": "Osborn",
            "email": "[email protected]",
            "confirmado": false
        },
        {
            "id": 1,
            "nombre": "Curry",
            "apellido": "Jefferson",
            "email": "[email protected]",
            "confirmado": true
        }
    ]
}

And this is what I get when I load up the page (part of it):

Image

And just in case it might be the problem, this is the directory of the JSON:

Second Image

0

2 Answers 2

4

Change

"ajax" : {"url":"/personas.json", "dataSrc":""}

to

"ajax" : {"url":"/personas.json", "dataSrc":"personas"}

By specifying dataSrc you tell that you use personas array from your personas.json as your data source.

See these for reference:

https://datatables.net/examples/ajax/custom_data_property.html https://datatables.net/examples/ajax/custom_data_flat.html

Also, as was already mentioned, your array [ opening bracket doesn't have a matching ] closing bracket.

Here's a working example with your data:

https://jsfiddle.net/onLuw2pa/165/ I've changed your JSON objects to arrays of values (by doing this you don't have to specify columns):

{
   "personas":[
      [
         0,
         "Aurelia",
         "Osborn",
         "[email protected]",
         false
      ],
      [
         1,
         "Curry",
         "Jefferson",
         "[email protected]",
         true
      ]
   ]
}

https://jsfiddle.net/onLuw2pa/169/ And here's an example which uses your exact JSON.

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

2 Comments

Hi dziraf, I've changed the code as you said, but now I get this message: "DataTables warning: table id=table_id - Requested unknown parameter '0' for row 0, column 0". The missing bracket is there, I just copied it wrong here. Also, I need the JSON to work as it is now. I can't change it unfortunately.
I've updated my answer to make it more precise. Look at jsfiddles, the second one uses your exact json.
0

It could be because your 'personas' data is malformed json. It is missing a closing array brace. It should be:

{
"personas": [
    {
        "id": 0,
        "nombre": "Aurelia",
        "apellido": "Osborn",
        "email": "[email protected]",
        "confirmado": false
    },
    {
        "id": 1,
        "nombre": "Curry",
        "apellido": "Jefferson",
        "email": "[email protected]",
        "confirmado": true
    }
  ]
}

1 Comment

Oh no, sorry, it does have it, I just copied it wrong.

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.