3

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

1 Answer 1

6

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

        }
     });
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This is not limited only to data retrieval. We need to customize other operations such as insert, delete, update and pagination. Is it possible to use MyApiClient in these operations in a similar way as you said, in place of the internal workings of jQuery Datatables?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.