DT Can't get data from JSON object

DT Can't get data from JSON object

SCRUMCRMSCRUMCRM Posts: 1Questions: 1Answers: 0
edited June 2021 in DataTables 1.10

I have below code in datatable to get JSON from API endpoint. The value of JSON object ("CustomerA") change everytime. Is there a way to feed the datatable? Thanks

JSON return from API

{ "CustomerA" : [
       {"ItemName" : "Tennis Ball",
        "ItemCode" : "T0001",
        "PurchaseDate" : "05/07/2020"
       },
       {"ItemName" : "Tennis Racquet",
        "ItemCode" : "R232",
        "PurchaseDate" : "05/07/2020"
       }
], 
 "CustomerB" : [
       {"ItemName" : "Basketball",
        "ItemCode" : "BX923",
        "PurchaseDate" : "05/07/2020"
       }
]
}

$('#dt').DataTable ({
ajax: { type: "POST",
           url: "http://www.xxx.com/api",
           contentType: "application/json",
           dataType: "json",
           dataSrc: ""
         },
columns : [
        {data : 'ItemName',
         data: 'ItemCode',
          data: 'PurchaseDate'
        ]
})

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    You wouldn't be able to have the two objects, "CustomerA" and "CustomerB", like that, as DataTables expects a single data object. Please see example here,

    Colin

Sign In or Register to comment.