假设我有数据表显示
1.电子邮件
2.文件
3.链接
我正在使用三个不同的API调用,在一个列表中显示所有这些,包括排序/分页/搜索等。
result = api/getEmails
result1 = api/getdocs
result2 = api/getlinks
如何在服务器端分页和排序的一个数据表中显示所有三个列表?
问题是分页和排序 - 根据所有三个API。
我是在客户端做的,从3个API获取所有数据并将其传递给Jquery Datatable然后处理排序/分页,但是当数据计数增加时,页面就会卡住。
答案 0 :(得分:1)
每个api调用都可以添加为下面的承诺:
var Promise = require('promise') // you need to get the npm library
// getAPI is your ajax call
function getApi(params, callback) {
// make ajax and use callback on the result
}
var promises = [];
// create promise - run this as many times as you need
var promise = new Promise(function (resolve, reject) {
getApi(params, function (result) {
// you can also add error handling of course
resolve(result)
})
})
promises.push(promise);
// resolve all promises in the array, refer to the docs here
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
new Promise.all(promises).then(function (results) {
// results is an array of all the results in your promises array
// in the order you put them in
});