I am trying to make a desktop like text input help using datatable.in table with keyboard navigation. For that I am dynamically changing the source and also change the header column. So far I have succeeded in that except getting dynamic column header.
For getting the column header based on the text input header, I am making an ajax call and getting the list of column header. the issue is during first call ajax returns undefined
but in the second call it shows the value. I understand this is pertaining to the asynchronous call but not sure how to handle this.
Below is my code snips.
AJAX call in external .js
function ajaxCall(ipUrl, callType = "POST", dataIn) {
return $.ajax({
url: ipUrl,
type: callType,
data: dataIn,
dataType: "json",
success: function (response) {
retData = response.data;
alert('success'+ retData);
return retData;
}, error: function (err) {
alert("fail" + JSON.stringify(err));
}, //error
});
//alert('in js'+ retData);
//return retData;
}
HTML Script tag
$("#btn").click( function (e) {
var tData = { action: 'getHeader',
csrfmiddlewaretoken: 'Gys4TSx3ODJLcMDuXlSvS7DopVZr1HWEDLg9AlJsARXp7wmUGAxxKwo6uLVXIrf2',
}
tmp = ajaxCall('dyncolheader','GET',tData) ;
if (tmp == undefined) {
alert('second call');
tmp = ajaxCall('dyncolheader','GET',tData) ;
alert('tmp' + tmp);
} else {
alert('else' + tmp);
}
});
Django View code
def dyncolheader(request):
hrdText = 'First,Second,Third'
if request.is_ajax and request.method == 'GET':
print('ajax call')
if request.GET.get('action') == 'getHeader':
print('get header')
return JsonResponse({ 'data': hrdText }, status=200)
return render(request, 'dyncolheader.html')