How to load an array of JSON objects to DataTables
How to load an array of JSON objects to DataTables
Hi:
I have an array of objects returned from an Ajax function as below:
var aDemoItems = [
{
"patientId":1,
"otherId":"LanTest101",
"firstName":"x1",
"lastName":"yLanTest101",
"gender":"M",
"dob":"10/16/1941",
"race":"Caucasian/White"
},
{
"patientId":2,
"otherId":"LanTest102",
"firstName":"x2",
"lastName":"yLanTest102",
"gender":"M",
"dob":"08/10/2005",
"race":"Caucasian/White"
},
{
"patientId":3,
"otherId":"Test1111",
"firstName":"x3",
"lastName":"yTest1111",
"gender":"M",
"dob":"08/13/2015",
"race":"Native Hawaian/Pacific Islander"
},
]
I want to load this data into an Datatable:
Here is mY HTML:
<table id="tblReportResultsDemographics" class="display" width="100%"></table>
Here is my load function: javascript:
function LoadCurrentReport(oResults) {
var aDemoItems = oResults.lDemographicItems; //
var jsonString = JSON.stringify(aDemoItems ) //for testing
//Load datatable
var oTblReport = $("#tblReportResultsDemographics")
oTblReport.DataTable ({
"data" : jsonString,
"columns" : [
{ "data" : "patientId" },
{ "data" : "otherId" },
{ "data" : "firstName" },
{ "data" : "lastName" },
{ "data" : "gender" },
{ "data" : "dob" },
{ "data" : "race" }
]
});
}
I always got this error "DatataTables warning: table id='tblReportResultsDemographics' requested unknown parameter 'patientId' for row 0 column 0"
whenever I tried to load the data.
Would anybody tell me what I'm dong wrong here?
I would really appreciate your help
Lmnguyen
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Answers
Just use
Don't give
data
a string - DataTables won't parse it. Just give it the array of data.Allan