1

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 ?

2
  • So, an empty for loop. That's all you tried? Commented Sep 15, 2013 at 16:30
  • I didn't get how to do it ? Because dataset is not a string that can be made inside for loop. Commented Sep 15, 2013 at 16:34

1 Answer 1

1

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]);
});
Sign up to request clarification or add additional context in comments.

1 Comment

append is not working :( flow is going inside foreach with json values but append is not working so I am not getting aDataSet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.