我有一个自定义 JavaScript 客户端,它可以使用 ajax 与远程 API 配合使用。
假设它是 MyApiClient,如下所示:
var MyApiClient = function() {
this.Invoke = function (config, callback) {
// ...
}
}
如何配置 jQuery Datatables,以便可以使用 MyApiClient 而不是 jQuery Datatables 内部提供的内置 ajax 工作?
也就是说,假设要加载远程数据,您需要这样调用客户端:
var client = new MyApiClient();
client.Invoke({ url: '/api/app/v1/some-entity/all', function(result, err) {
// ...
}
});
谢谢你了
最佳答案
使用ajax
选项来定义一个函数以通过您自己的 Ajax 调用检索数据。
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.
例如:
$('#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);
}
});
}
});
关于jquery数据表自定义ajax客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41313731/