user2767347
user2767347

Reputation: 175

Generate DataTables Dynamically from json array

I am getting JSON data from a php file and want to display it with DataTables. So I am creating DataTables dynamically. But I am getting problem in making following data from the data coming in JSON.

var aDataSet = [
                ['Trident','Internet Explorer 4.0','Win 95+','4','X'],
                ['Trident','Internet Explorer 5.0','Win 95+','5','C']
            ];

I want each entry in this Dataset dynamically. I am using "for loop".

for(i=0;i<count;i++)
{
}

JSON Data coming from PHP file:

{
    "data": [
        {
            "staff_id": "1",
            "staff_name": "Chinu",
            "rig_days": "80",
            "manager_id": "2",
            "grade": "8",
        },
        {
            "staff_id": "2",
            "staff_name": "John",
            "rig_days": "90",
            "manager_id": "3",
            "grade": "10",
        }
]
}

How it should be placed in for loop so that it works well ?

Upvotes: 1

Views: 695

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

If you have control over PHP you could change how the JSON is encoded to send a multidimensional array for the data property, but it's not that difficult to do with JS anyway:

var aDataSet = [];
json.data.forEach(function (elem) {
    aDataSet.push([elem.staff_id, elem.staff_name,
       elem.rig_days, elem.manager_id, elem.grade]);
});

Upvotes: 1

Related Questions