I need to populate https://datatables.net/ from 2 different api endpoints from jQuery.
First endpoint (Owner Api) retrieves only "Driver name", the second endpoint (VehicleDetail Api) retrieves "Vehicle Name and Number Plate"
The database on the server is as follow, and it's a 1 to many relationship (1 owner may have multiple vehicles)
1. TableOwner :
Fields
-----------
Id
Name
2. TableVehicleDetail :
Fields
-----------
Id
Name
NumberPlate
OwnerId (Foreign Key)
My datatable has 3 columns (Driver Name, VehicleName, Number Plate), and I need to populate it from the two api endpoints.
Driver Name data must come from endpoint api called Owner,
VehicleName, Number Plate must come from endpoint api called VehicleDetail
I read that Datatable has a property called 'ajax source', I tried it but I couldn't achieve.
Regarding jQuery API, I'm using jquery Fetch API to retrieve data from endpoints api.
Below is the code I use. I have two similar code on the same page for both endpoints (Owner and VehicleDetail api).
// ******** Owner Start
getData();
async function getData() {
const response = await fetch('https://localhost:44389/api/owners');
const settings = {
method: 'get',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
}
};
const owner = await response.json();
var arrayToString = JSON.stringify(Object.assign({}, owner)); // convert array to string
var stringToJsonObject = JSON.parse(arrayToString); // convert string to json object
return stringToJsonObject;
}
getData().then(owner => {
owner;
$.each(owner, function (index, value) {
console.log(value.name); // Here data can be displayed on console.log
});
});
// ******** Owner End
// ******** VehicleDetail Start
getData();
async function getData() {
const response = await fetch('https://localhost:44389/api/vehicleDetail');
const settings = {
method: 'get',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
}
};
const vehicle = await response.json();
var arrayToString = JSON.stringify(Object.assign({}, vehicle)); // convert array to string
var stringToJsonObject = JSON.parse(arrayToString); // convert string to json object
return stringToJsonObject;
}
getData().then(vehicle => {
vehicle;
$.each(vehicle, function (index, value) {
console.log(value.name); // Here data can be displayed on console.log
});
});
// ******** VehicleDetail End