0

I'm doing bulk update of some entries that I have using spring boot and jquery ajax to save the entries, and I want reload the datatable before ajax hits success function

jQuery save function:

function saveBulkUpload() {
    var form = $('#bulkUploadForm')[0];
    var formData = new FormData(form);
    $.ajax({
        url: '/bulkUpload/save',
        method: 'POST',
        data: formData,
        cache : false,
        enctype: 'multipart/form-data',
        contentType: false,
        processData: false,
    }).done(function(data, textStatus, jqXHR){
            bulkUploadDataTable.ajax.reload();
    });         
}

jQuery DataTable:

function showDataTable() {
    bulkUploadDataTable = $('#bulkUploadDataTable').DataTable({
        emptyTable      : 'No record of vendor found',
        lengthChange    : true,
        processing      : true,
        ajax : {
            'url'   : '/bulkUpload/findAll',
            'type'  : 'POST'
        },
        columns : [
            {'data' : 'selectedModuleName'},
            {'data' : 'fileName'},
            {'data' : 'remark'}
        ],
        searching   : false,
        buttons : ['excel', 'csv', 'pdf', 'copy'],
        scrollCollapse: true
    });
}

I've tried:

//1
    $.ajax({
        url: '/bulkUpload/save',
        method: 'POST',
        data: formData,
        cache : false,
        enctype: 'multipart/form-data',
        contentType: false,
        processData: false,
        beforeSend: function() {    
            bulkUploadDataTable.ajax.reload();
            },
    })

and

//2
$.ajax({
        url: '/bulkUpload/save',
        method: 'POST',
        data: formData,
        cache : false,
        enctype: 'multipart/form-data',
        contentType: false,
        processData: false,
    }).always(bulkUploadDataTable.ajax.reload())

but both doesn't seem to work.

I just want to reload the datatable once before the success function of ajax because values in database changes during uploading and after uploading is done.

Please help me here...

Cybertronian
  • 473
  • 1
  • 8
  • 17
  • 1
    Have you tried calling it before calling `$.ajax`? – ahwayakchih Oct 15 '19 at 15:42
  • @ahwayakchih it will be of no use because there are no updates before `url: '/bulkUpload/save',` – Cybertronian Oct 15 '19 at 15:45
  • Oh, sorry. I thought you want to update table before ajax call changes data. So, does that mean you want to update table after data is changed, but before... before what happens? Because you wrote: `values in database changes during uploading and after uploading is done`. Does that mean you want to update table during ajax call and after it? – ahwayakchih Oct 15 '19 at 15:49
  • @ahwayakchih yup totally – Cybertronian Oct 15 '19 at 15:50
  • So i'm guessing you want to show some kind of progress bar or uploaded percent? Since `jquery.ajax` is asynchronous by default, you could try to start some interval before calling ajax, and stop it after success/error, but that's a bit "hacky". If it's a progress bar you need, check answers at https://stackoverflow.com/questions/15410265/file-upload-progress-bar-with-jquery – ahwayakchih Oct 15 '19 at 15:55

0 Answers0