我使用datatables插件来渲染一些结果,这里的复杂性是我必须遍历Sharepoint中的一些列表,然后进行查询,然后将每个结果追加到最终结果中,然后显示最终结果
当我调试foreach(结果)时,我可以看到结果被追加,到目前为止我得到了13个结果项。
但是当调试器到达datatable.add方法时,该数组为空,并且没有任何内容呈现。
function GetData(billCycleId, clientCode, jobCodes, engagementCode) {
var enhanceFunctions = [
function(searchResultRow) {
return spService.AddHyperLinkOnFields(searchResultRow, config.HyperLinks);
},
function(searchResultRow) {
return spService.AddPresenceOnFields(searchResultRow, config.UserFields);
},
function(searchResultRow) {
return spService.FormatDateFields(searchResultRow, config.DateFields, generalConfig.DateTimeFormat);
},
function(searchResultRow) {
return spService.AddImageMapping(searchResultRow, config.ImageFields);
},
function(searchResultRow) {
return spService.FormatNumberFields(searchResultRow, config.NumberFields);
}
];
var selectProperties = spService.TransformFieldsToSelectProperties(config.Fields);
var extendedSelectProperties = selectProperties.slice(); // copy array
var hyperLinkedProperties = spService.TransformFieldsToSelectProperties(config.HyperLinks)
extendedSelectProperties = extendedSelectProperties.concat(hyperLinkedProperties);
spService.GetAllListsFromWeb()
.then(function(lists){
var listEnumerator = lists.getEnumerator();
var result =[];
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
var title = oList.get_title();
var id = oList.get_id();
if(title.indexOf("Bill Cycles") !== -1){
// Get data from SP
GetRelatedBillCyclesFromList(id, extendedSelectProperties, billCycleId, clientCode, jobCodes, engagementCode, enhanceFunctions)
.then(function (data) {
var trimmedData = spService.SpSearchQuery.TrimSearchResultsToSelectProperties(data, selectProperties);
// Add data to dataTable
trimmedData.forEach(function(item){ // loop over source array
result.push(item); //append to result array
});
})
.catch (function (message) {
vm.Name = "Error";
vm.ValidDataLoaded = true;
});
}
//Do something with oList.
}
var dataTable = $(tableSelector).DataTable();
dataTable.clear().rows.add(result).columns.adjust().draw(); // Resize columns based on new data sizes
vm.ValidDataLoaded = true;
})
}
function getAllListsFromWeb(){
var deferred = $q.defer();
var context = SP.ClientContext.get_current();
var web = context.get_web();
var lists = web.get_lists();
context.load(lists);
context.executeQueryAsync(
function() {
$log.info("Successfully retrieved list item result");
deferred.resolve(lists);
},
function(error, errorInfo) {
$log.warn("Retrieving list item result failed");
deferred.reject(errorInfo);
}
);
return deferred.promise;
}
更新1
也尝试了这个,但没有工作
function GetData(billCycleId, clientCode, jobCodes, engagementCode) {
var enhanceFunctions = [
function(searchResultRow) {
return spService.AddHyperLinkOnFields(searchResultRow, config.HyperLinks);
},
function(searchResultRow) {
return spService.AddPresenceOnFields(searchResultRow, config.UserFields);
},
function(searchResultRow) {
return spService.FormatDateFields(searchResultRow, config.DateFields, generalConfig.DateTimeFormat);
},
function(searchResultRow) {
return spService.AddImageMapping(searchResultRow, config.ImageFields);
},
function(searchResultRow) {
return spService.FormatNumberFields(searchResultRow, config.NumberFields);
}
];
var selectProperties = spService.TransformFieldsToSelectProperties(config.Fields);
var extendedSelectProperties = selectProperties.slice(); // copy array
var hyperLinkedProperties = spService.TransformFieldsToSelectProperties(config.HyperLinks)
extendedSelectProperties = extendedSelectProperties.concat(hyperLinkedProperties);
var result =[];
var data = spService.GetAllListsFromWeb()
.then(function(lists){
var listEnumerator = lists.getEnumerator();
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
var title = oList.get_title();
var id = oList.get_id();
if(title.indexOf("Bill Cycles") !== -1){
// Get data from SP
GetRelatedBillCyclesFromList(id, extendedSelectProperties, billCycleId, clientCode, jobCodes, engagementCode, enhanceFunctions)
.then(function (data) {
var trimmedData = spService.SpSearchQuery.TrimSearchResultsToSelectProperties(data, selectProperties);
// Add data to dataTable
trimmedData.forEach(function(item){ // loop over source array
result.push(item); //append to result array
});
})
.catch (function (message) {
vm.Name = "Error";
vm.ValidDataLoaded = true;
});
}
}
return result;
})
var resultadata = data;
var dataTable = $(tableSelector).DataTable();
dataTable.clear().rows.add(resultdata).columns.adjust().draw(); // Resize columns based on new data sizes
vm.ValidDataLoaded = true;
}
答案 0 :(得分:1)
由于数据似乎是一个承诺,你可以试试这个:
//not saving it to data
spService.GetAllListsFromWeb()
.then(function (lists) {
var listEnumerator = lists.getEnumerator();
return Promise.all(
(function(){
var promises = [];
while (listEnumerator.moveNext()) {
var oList = listEnumerator.get_current();
var title = oList.get_title();
var id = oList.get_id();
if (title.indexOf("Bill Cycles") !== -1) {
// Get data from SP !!! this is also async and returns a promise
// add the promise to promises array and wait for all to finish
// look above in Promise.all
promises.push(
GetRelatedBillCyclesFromList(
id,
extendedSelectProperties,
billCycleId,
clientCode,
jobCodes,
engagementCode,
enhanceFunctions
)
.then(function (data) {
return spService
.SpSearchQuery
.TrimSearchResultsToSelectProperties(
data,
selectProperties
);
})
);
}
}
return promises
})() //IIFE returning an array of promises
);
})
.then(
function(data){
console.log("got data:",JSON.stringify(data,undefined,2));
var resultadata = data;
var dataTable = $(tableSelector).DataTable();
dataTable.clear().rows.add(resultdata).columns.adjust().draw(); // Resize columns based on new data sizes
vm.ValidDataLoaded = true;
}
);
您应该查看承诺是什么以及如何在JavaScript中使用它。
由于您编写的函数始终是同步的(只有一个运行代码的线程),因此您的函数需要立即返回一个值(非阻塞)。
当一个函数需要发出网络请求,文件IO或长时间运行的进程时,你会立即返回一个promise。 promise是一个对象,它有一个名为then
的函数,它带有2个处理函数
所以,当你尝试做类似的事情时:
var result = [];
var later = x => new Promise(r=>setTimeout(r(x),100));
[1,2,3,4,5]
.map(
x =>
later(x)
.then(
x => {
console.log("resolved with:",x);
result.push(x);
return x;
}
)
);
console.log("first output",result);
//the output will be:
// first output []
// resolved with: 1
// resolved with: 2
// resolved with: 3
// resolved with: 4
// resolved with: 5
你会看到,当你尝试用结果做某事时,没有任何承诺被解决,所以它是空的。在我放在这里的答案中,我使用Promise.all
来解析所有承诺,然后使用dataTable的解析值。