load array of json

load array of json

denizdianadenizdiana Posts: 15Questions: 8Answers: 0

Hello everyone,
I have a json variable :

var data=  {
"events": [
    {
        "name": "Jack",
        "date": "05-03-2020",
        "time": "18:10:11",
        "action": "enter"
    },
    {
        "name": "Jane",
        "date": "05-03-2020",
        "time": "08:40:11",
        "action": "enter"
    }
]

}

And I want to load this into datatable.

    var dtable = $('#table').DataTable(
            {

             data : data,

              columns: [

                    { title: 'events.name' },
                    { title: 'events.action' },
                    { title: 'events.date' }


              ]

            });

But it doesn't work. I can see the table but it is empty. What is the problem? Thank you so much.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited April 2020 Answer ✓

    Try changing line 4 to data : data.events,. It should be able to find the row data then.

    You will want to use columns.data to define your object based data source. column.title will set the header titles. Maybe something like this:

              columns: [
     
                    { data: 'name',  title: 'Name'  },
                    { data: 'action', title: 'Action'  },
                    { data: 'date', title: 'Date' }
               ]
    

    Kevin

  • denizdianadenizdiana Posts: 15Questions: 8Answers: 0

    I change it but it doesnt work it gives me
    "DataTables warning: table id=Person - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4" error.

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    Sorry, you were too quick to see me edited response :smile:

    Re-read my response.

    Kevin

This discussion has been closed.