为多个AJAX调用创建动态变量(SharePoint Online)



我使用以下代码生成jquery数据表,其中包含从SharePoint Online JSON on AJAX调用返回的数据。由于我的SPO列表有超过50万行,而且SPO的查看阈值限制为5000,因此我使用RESTneneneba API多个日期范围过滤器进行多个ajax调用,并连接结果。然后将连接的结果作为数据表插件的数据源进行传递。

请帮助缩短以下年份变量的代码,并动态输入日期范围以进行多个AJAX调用。我猜这个过程是先读取当前年份,生成当前年份的第一个和最后一个日期,然后按降序创建新的变量,直到2005年。

var results;
var allResults = [];
$(document).ready(function () {
load();
});
function load() {
var year2021 = $.ajax({    
url: "SPO_Site/_api/web/lists/getbytitle('SPO_List')/items?$top=5000&$select=*,EncodedAbsUrl&$filter=(Date%20ge%20datetime%272020-12-31T00:00:00.000Z%27)%20and%20(Date%20le%20datetime%272021-12-31T00:00:00.000Z%27)",    
type: "GET", dataType: "json", headers: {"accept": "application/json;odata=verbose"},    
success: mySuccHandler, error: myErrHandler}); 
var year2020 = $.ajax({    
url: "SPO_Site/_api/web/lists/getbytitle('SPO_List')/items?$top=5000&$select=*,EncodedAbsUrl&$filter=(Date%20ge%20datetime%272019-12-31T00:00:00.000Z%27)%20and%20(Date%20le%20datetime%272020-12-31T00:00:00.000Z%27)",    
type: "GET", dataType: "json", headers: {"accept": "application/json;odata=verbose"},    
success: mySuccHandler, error: myErrHandler}); 

.
.
.
.
.
var year2005 = $.ajax({    
url: "SPO_Site/_api/web/lists/getbytitle('SPO_List')/items?$top=5000&$select=*,EncodedAbsUrl&$filter=(Date%20ge%20datetime%272004-12-31T00:00:00.000Z%27)%20and%20(Date%20le%20datetime%272005-12-31T00:00:00.000Z%27)",    
type: "GET", dataType: "json", headers: {"accept": "application/json;odata=verbose"},    
success: mySuccHandler, error: myErrHandler});          

function mySuccHandler(a) {
results = a.d.results;
if (allResults.length > 0)
allResults = allResults.concat(results);
else
allResults = results;        
}
function myErrHandler(data, errorCode, errorMessage) {
console.log("Could not complete call: " + errorMessage);        
};
$.when(year2021, year2020, year2019, year2018, year2017, year2016, year2015, year2014, year2013, year2012, year2011, year2010, year2009, year2008, year2007, year2006, year2005).done(function(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17){
$('#table_id').DataTable({
data:allResults,
dom: 'Bfrtip',
columns: [
{ data: "Reg" },
{ data: "EncodedAbsUrl",
"render": function (data, type, full)
{return '<a href="'+data+'" target="_blank">View</a>';}             
}
]
});
});  
};

当您发现自己复制粘贴代码时,请立即停止并编写一个函数和一个循环。

当您发现自己复制粘贴代码15次时,您应该在14次之前停止编写函数和循环。

功能:

function getItemsByYear(year, topRows=5000) {
return $.ajax({
type: "GET",
headers: {
Accept: "application/json;odata=verbose"
},
url: "SPO_Site/_api/web/lists/getbytitle('SPO_List')/items?" + $.param({
$top: topRows,
$select: '*,EncodedAbsUrl',
$filter: `(Date ge datetime'${year}-01-01T00:00:00.000Z') and (Date lt datetime'${year + 1}-01-01T00:00:00.000Z')`
}),
})
.then(function (response) {
return response.d.results;
})
.fail(function (data, errorCode, errorMessage) {
console.log("Could not complete call: " + errorMessage);        
});
}

循环:

$(function () {
let currentYear = (new Date()).getFullYear(),
requests = [];
for (let counter = 0; counter < 15; counter++) {
requests.push(getItemsByYear(currentYear - counter));
}
$.when.apply($, requests).done(function (itemsByYear) {
$('#table_id').DataTable({
data: itemsByYear.flat(),
dom: 'Bfrtip',
columns: [
{ data: "Reg" },
{ data: "EncodedAbsUrl", "render": function (data, type, full)
{return '<a href="'+data+'" target="_blank">View</a>';}             
}
]
});
});
});  

相关内容

  • 没有找到相关文章

最新更新