i want to choose datatable ajax url whether its containingdata1 , containingdata2 or containingdata3 .
here is my code:
$(document).ready(function() {
var $table = $('#datatable1');
$table.DataTable({
"columnDefs": [
{"className": "dt-center", "targets": "_all"}
],
if(containdata1){
ajax: {
url: "/api/dunapi/"+containdata1,
type: 'GET'
},
if(containdata2){
ajax: {
url : "/api/dunapi/"+containdata2,
type : 'GET'
},
}
if(containdata3){
ajax: {
url : "/api/dunapi/"+containdata3,
type : 'GET'
},
}
i want to do it like that if it can possibly be done
If you mean that you want to merge data from many data source, then it would be best to gather the data outside of DataTable. Use jQuery's ajax() function (Or any JavaScript client like axios) to get all the data, merge it together into a single object, and pass into DataTables's data option.
Like:
var data = []
if(containdata1){
await axios.get("/api/dunapi/"+containdata1).then(res => {
data = data.concat(res.data)
})
}
if(containdata2){
await axios.get("/api/dunapi/"+containdata2).then(res => {
data = data.concat(res.data)
})
}
if(containdata3){
await axios.get("/api/dunapi/"+containdata3).then(res => {
data = data.concat(res.data)
})
}
then when define your datatable:
{
// datatable options
data: data
.....
}
Note: you should add async before ready function because we are using await before each request:
$(document).ready(async function() {
......
}