0

I tried displaying data into datatables with ajax source json, before there was no problem but after I found the nested json array I was a little confused to implement it, so how do I apply the json I have into datatables . this is my data

Config datatables

var myTable = $('#myTable').DataTable({
    ajax: base_url+"api/data/list",
    responsive: true,       
    columns: [
            { "data": "", "sClass": "text-center", responsivePriority: 0 },
            { "data": "first_name", "sClass": "text-center", responsivePriority: 1 },
            { "data": "last_name", "sClass": "text-left"},
            { "data": "birth_date", "sClass": "text-left",  responsivePriority: 2 },
            { "data": "birth_place", "sClass": "text-left" },               
    ],              
    language: {
        searchPlaceholder: 'Search...',
        sSearch: '',
        zeroRecords: 'No data',
    }
})  

Result ajax json

{
  "data": [
    {
      "1": {
        "first_name": "Jonh",
        "last_name": "Connor",
        "birth_date": "1991-11-05",
        "birth_place": "USA"
      },
      "2": {
        "first_name": "Uka",
        "last_name": "Uka",
        "birth_date": "2013-01-06",
        "birth_place": "Austria"
      }
    }
  ]
}

which I expect the result as the following table

| NO  | first_name | last_name | birth_date | birth_place |
|  1  |    Jonh    |   Connor  | 1991-11-05 |    USA      | 
|  2  |    Uka     |    Uka    | 2013-01-06 |  Austria    |     
4
  • What is your question? Commented Aug 13, 2018 at 13:42
  • @Milney I can't implement json data into datatables with nested arrays Commented Aug 13, 2018 at 14:09
  • That is still a statement not a question... Commented Aug 13, 2018 at 14:27
  • @Milney I try to update my question Commented Aug 13, 2018 at 14:34

1 Answer 1

1

IDK if this helps but...

myTable.data[0].1.first_name

maybe?

it an object containing your object of arrays containing more objects. Its like JSON inception.

Update:

Pretty sure the ID thing you got going on there is messing it up. It thinks is a column name. so column 1 turn into column 2. Just put the ID in the data.

try this,

HTML

<!DOCTYPE html>
<html>
<head>
  <!-- Data Tables -->
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
  </head>
<body>

<p id="demo"></p>
<br/>
<table id=example>
<thead>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Birth Date</th>
                <th>Birth Place</th>
            </tr>
        </thead>
</table>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

        <!-- Data Tables -->
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script scr="http://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"></script>

</body>
</html>

Script

let data = {
  "data": [
    {
            "record_id": "1",
        "first_name": "Jonh",
        "last_name": "Connor",
        "birth_date": "1991-11-05",
        "birth_place": "USA"
      },
        {
            "record_id": "2",
        "first_name": "Uka",
        "last_name": "Uka",
        "birth_date": "2013-01-06",
        "birth_place": "Austria"
      }
  ]
};

$(document).ready(function() {
    $('#example').DataTable( {
        data: data.data,
        columns: [
        { "data": "record_id", "sClass": "text-center", responsivePriority: 0 },
            { "data": "first_name", "sClass": "text-center", responsivePriority: 1 },
            { "data": "last_name", "sClass": "text-left"},
            { "data": "birth_date", "sClass": "text-left",  responsivePriority: 2 },
            { "data": "birth_place", "sClass": "text-left" },
        ]
    } );
} );

JSfiddle: https://jsfiddle.net/scuba_steve/n86z2r4u/26/

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

1 Comment

thanks, what if the data is a lot means having to use a loop

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.