Mohammad Fazeli
Mohammad Fazeli

Reputation: 817

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

Upvotes: 3

Views: 2607

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58880

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);

        }
     });
  }
});

Upvotes: 6

Related Questions