Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery datatables custom ajax client

I have a custom javascript client that works with a remote API using ajax.

Suppose it to be MyApiClient like below:

var MyApiClient = function() {
   this.Invoke = function (config, callback) {
     // ...
   }
}

How can I configure jQuery Datatables so that I can use MyApiClient rather than the built-in ajax working that jQuery Datatables provides internally?

That is, suppose for loading your remote data, you need to call your client this way:

var client = new MyApiClient();

client.Invoke({ url: '/api/app/v1/some-entity/all', function(result, err) {
     // ...
  }
});

Thank you already

like image 512
Mohammad Fazeli Avatar asked Apr 17 '26 04:04

Mohammad Fazeli


1 Answers

Use ajax option to define a function to retrieve the data through your own Ajax call.

As a function, making the Ajax call is left up to yourself allowing complete control of the Ajax request. Indeed, if desired, a method other than Ajax could be used to obtain the required data, such as Web storage or a Firebase database.

When the data has been obtained from the data source, the second parameter (callback here) should be called with a single parameter passed in - the data to use to draw the table.

For example:

$('#example').DataTable( {
  "ajax": function (data, callback, settings) {
     var client = new MyApiClient();
     client.Invoke({ url: '/api/app/v1/some-entity/all', function(result, err){

           // Pass the data to jQuery DataTables
           callback(result);

        }
     });
  }
});
like image 151
Gyrocode.com Avatar answered Apr 19 '26 14:04

Gyrocode.com