空值未被替换为 jQuery/Conditional
Null Value not Getting Replaced by jQuery/Conditional
我正在 运行 解决我正在提取的一些数据对象的问题,这些对象没有附加任何数据,因此它发布“-null”来代替空数据(是预期的行为)。
我已经实现了条件 if 语句 if (results == null){ $('#report-summary').text("There were no notes");}
,但仍然返回空值?
这是我的 已更新 JS,它仍然 returns null;
function loadData(url) {
url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('WeeklyReport')/items?$select=DeliverablesSubmitted,MajorTasks,PersonnelActions,SupportRequest,ResourceRequest,Team,Training,Upcoming,WeekOf,TravelODC";
return fetch(url, { headers: { accept: "application/json; odata=verbose" } }) // make request
.then((r) => {
if (!r.ok) throw new Error("Failed: " + url); // Check for errors
return r.json(); // parse JSON
})
.then((data) => data.d.results);
}
loadData()
.then((results) => {
const data = results;
var listContent = [];
for (var i = 0; i < data.length; i++) {
listContent += '<li data-weekOf="'+data[i].WeekOf+'">';
listContent += '<h2 id="teamname">' + data[i].Team +'</h2>';
listContent += '<h4 id="tasks"> Tasks </h4>';
listContent += '<ul>' + "- " + data[i].MajorTasks + '</ul>';
listContent += '<h4 id="deliverables"> Deliverables </h4>';
listContent += '<ul>' + "- " + data[i].Deliverables + '</ul>';
listContent += '<h4 id="actions"> Actions </h4>';
listContent += '<ul>' + "- " + data[i].Actions + '</ul>';
listContent += '<h4 id="events"> Upcoming Events </h4>';
listContent += '<ul>' + "- " + data[i].Upcoming + '</ul>';
listContent += '<h4 id="training"> Training </h4>';
listContent += '<ul>' + "- " + data[i].Training + '</ul>';
listContent += '<h4 id="resource"> Resource Request </h4>';
listContent += '<ul>' + "- " + data[i].Resource + '</ul>';
listContent += '<h4 id="support"> Support Request </h4>';
listContent += '<ul>' + "- " + data[i].Support + '</ul>';
listContent += '<h4 id="travel"> Travel </h4>';
listContent += '<ul>' + "- " + data[i].Travel + '</ul>';
listContent += '</li>';
}
if (results !== null){
$('#report-summary').html(listContent);
}else{
$('#report-summary').text("There were no notes");
}
})
.catch((err) => {
alert("Error: " + err);
console.error(err);
});
我有点走对了路,为了达到我的预期结果,我不得不像这样在原始 for 循环中嵌套条件:
for (var i = 0; i < data.length; i++) {
listContent += '<li data-weekOf="'+data[i].WeekOf+'">';
listContent += '<h2>' + data[i].Team +'</h2>';
listContent += '<h4> Tasks </h4>';
if(data[i].Tasks !== null){
listContent += '<ul>' + "- " + data[i].Tasks + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Deliverables </h4>';
if(data[i].Deliverables !== null){
listContent += '<ul>' + "- " + data[i].Deliverables + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Personnel Actions </h4>';
if(data[i].Actions !== null){
listContent += '<ul>' + "- " + data[i].Actions + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Finance (Billable) </h4>';
if(data[i].Billable !== null){
listContent += '<ul>' + "- " + data[i].Billable + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Finance (Non-Billable) </h4>';
if(data[i].NonBillable !== null){
listContent += '<ul>' + "- " + data[i].NonBillable + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Upcoming Events </h4>';
if(data[i].UpcomingEvents !== null){
listContent += '<ul>' + "- " + data[i].UpcomingEvents + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Training </h4>';
if(data[i].Training !== null){
listContent += '<ul>' + "- " + data[i].Training + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Resource Request </h4>';
if(data[i].Resource !== null){
listContent += '<ul>' + "- " + data[i].Resource + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Support Request </h4>';
if(data[i].Support !== null){
listContent += '<ul>' + "- " + data[i].Support + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '</li>';
}
$('#report-summary').html(listContent);
我正在 运行 解决我正在提取的一些数据对象的问题,这些对象没有附加任何数据,因此它发布“-null”来代替空数据(是预期的行为)。
我已经实现了条件 if 语句 if (results == null){ $('#report-summary').text("There were no notes");}
,但仍然返回空值?
这是我的 已更新 JS,它仍然 returns null;
function loadData(url) {
url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('WeeklyReport')/items?$select=DeliverablesSubmitted,MajorTasks,PersonnelActions,SupportRequest,ResourceRequest,Team,Training,Upcoming,WeekOf,TravelODC";
return fetch(url, { headers: { accept: "application/json; odata=verbose" } }) // make request
.then((r) => {
if (!r.ok) throw new Error("Failed: " + url); // Check for errors
return r.json(); // parse JSON
})
.then((data) => data.d.results);
}
loadData()
.then((results) => {
const data = results;
var listContent = [];
for (var i = 0; i < data.length; i++) {
listContent += '<li data-weekOf="'+data[i].WeekOf+'">';
listContent += '<h2 id="teamname">' + data[i].Team +'</h2>';
listContent += '<h4 id="tasks"> Tasks </h4>';
listContent += '<ul>' + "- " + data[i].MajorTasks + '</ul>';
listContent += '<h4 id="deliverables"> Deliverables </h4>';
listContent += '<ul>' + "- " + data[i].Deliverables + '</ul>';
listContent += '<h4 id="actions"> Actions </h4>';
listContent += '<ul>' + "- " + data[i].Actions + '</ul>';
listContent += '<h4 id="events"> Upcoming Events </h4>';
listContent += '<ul>' + "- " + data[i].Upcoming + '</ul>';
listContent += '<h4 id="training"> Training </h4>';
listContent += '<ul>' + "- " + data[i].Training + '</ul>';
listContent += '<h4 id="resource"> Resource Request </h4>';
listContent += '<ul>' + "- " + data[i].Resource + '</ul>';
listContent += '<h4 id="support"> Support Request </h4>';
listContent += '<ul>' + "- " + data[i].Support + '</ul>';
listContent += '<h4 id="travel"> Travel </h4>';
listContent += '<ul>' + "- " + data[i].Travel + '</ul>';
listContent += '</li>';
}
if (results !== null){
$('#report-summary').html(listContent);
}else{
$('#report-summary').text("There were no notes");
}
})
.catch((err) => {
alert("Error: " + err);
console.error(err);
});
我有点走对了路,为了达到我的预期结果,我不得不像这样在原始 for 循环中嵌套条件:
for (var i = 0; i < data.length; i++) {
listContent += '<li data-weekOf="'+data[i].WeekOf+'">';
listContent += '<h2>' + data[i].Team +'</h2>';
listContent += '<h4> Tasks </h4>';
if(data[i].Tasks !== null){
listContent += '<ul>' + "- " + data[i].Tasks + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Deliverables </h4>';
if(data[i].Deliverables !== null){
listContent += '<ul>' + "- " + data[i].Deliverables + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Personnel Actions </h4>';
if(data[i].Actions !== null){
listContent += '<ul>' + "- " + data[i].Actions + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Finance (Billable) </h4>';
if(data[i].Billable !== null){
listContent += '<ul>' + "- " + data[i].Billable + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Finance (Non-Billable) </h4>';
if(data[i].NonBillable !== null){
listContent += '<ul>' + "- " + data[i].NonBillable + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Upcoming Events </h4>';
if(data[i].UpcomingEvents !== null){
listContent += '<ul>' + "- " + data[i].UpcomingEvents + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Training </h4>';
if(data[i].Training !== null){
listContent += '<ul>' + "- " + data[i].Training + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Resource Request </h4>';
if(data[i].Resource !== null){
listContent += '<ul>' + "- " + data[i].Resource + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '<h4> Support Request </h4>';
if(data[i].Support !== null){
listContent += '<ul>' + "- " + data[i].Support + '</ul>';
}else{
listContent += '<ul>' + "- There were no notes attached to this section." + '</ul>';
}
listContent += '</li>';
}
$('#report-summary').html(listContent);