jquery数据表自定义ajax客户端

标签 jquery ajax datatables customization

我有一个自定义 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/

相关文章:

javascript - 在 html css javascript jquery 中创建导航菜单?

javascript - 从 js 调用不同当前文件中的类

javascript - 使用 json 对象预填充表单的值

javascript - Jquery 克隆函数

php - 通过 JavaScript 或 PHP 使用 Google map API?

javascript - 如何解决if else条件下的ajax调用?

javascript - 将数据表放入同一页的两个表中

javascript - 创建通用 Javascript/Jquery ajax 函数

jquery - DataTables.net columns.width 不起作用

javascript - 使用 PHP 数组作为 DataTables 的数据源