John
John

Reputation: 1751

jQuery Refresh table after is $.ajax send

I have one $.ajax function that needs to refresh table after the request is sent and after success, the request is received it needs to refresh table again. I successfully refresh table after success is received but I don't know how to refresh table when ajax is send and is waiting for success or error to be received.

               /* SHOW - message */
                sweetAlert({
                    title: 'Are You Sure?',
                    html: message+" <b>"+window.server+"</b> Server?",
                    type: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: 'Yes'
                    }).then(function() {
                        /* SERVER - data */
                        var data     = {};
                        data.id      = window.id;
                        data.type    = window.type;

                        /* GET - server install */
                        $.ajax({
                            type: 'POST',
                            url: '/api/server_manage',
                            data: data,
                            dataType: 'json',
                            success: function(res){
                                console.log('refresh table...');
                                $('#basicDataTable').dataTable().fnDraw(false);
                            }
                        })
                    }).catch(swal.noop);

So in short it needs to be done like this:

ajax request send....and waiting... refresh table... ajax request received success or error... refresh table...

If i put refresh before or after ajax it is not good because it reads datatables from mysql database value that needs to be displayed in datatables..and its value sets the ajax when is called

Upvotes: 0

Views: 543

Answers (1)

darklightcode
darklightcode

Reputation: 2772

DataTables can feed as shown in the example from https://datatables.net/examples/data_sources/server_side.html and https://datatables.net/reference/option/ajax

Change your code as follows:

           /* SHOW - message */
            sweetAlert({
                title: 'Are You Sure?',
                html: message+" <b>"+window.server+"</b> Server?",
                type: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes'
                }).then(function() {
                    /* SERVER - data */
                    var data     = {};
                    data.id      = window.id;
                    data.type    = window.type;

                    /* GET - server install */

                    $('#basicDataTable').DataTable( {
                        "processing": true,
                        "serverSide": true,
                        "ajax": {
                           "url": "/api/server_manage",
                           "type": "POST"
                           "data": data,
                           "dataType": "json"
                        }
                    });

                }).catch(swal.noop);

Upvotes: 1

Related Questions