我正在尝试对从数据库中提取的一些数据行做一些事情。我要做的第一件事是将保存该行的日期与今天的日期进行比较,如果保存该行的日期已过,我需要将其删除,同时将相关日期附加到表格中。当遍历数据库行的 for 循环完成后,我需要检查表中的行数。如果它是空的,因为只有过去的日期行并且它们都已被删除,我需要向用户显示一条解释空表的消息,否则我不需要显示任何内容。这是我尝试结合使用三元运算符和获取表行数的 jquery 方法的地方。出于某种原因,无论行数是零还是更多,三元运算符的第二个表达式都会起作用。因此,即使行数为零,代码也会表现得好像存在行,并且不会显示针对空表的消息。为了便于阅读,我省略了一些代码。
//HTML
<table id="volunteerListTable">
<tbody id="list">
</tbody>
</table>
//JQUERY
if(results.length !== 0){
// SAVE TODAYS DATE IN A VARIABLE CALLED "todaysDate"
//LOOP THROUGH DATABASE QUERY RESULTS
for (var i = 0; i < results.length; i++) {
//SAVE THE DATE SPECIFIED IN THE ROW AS A VARIABLE CALLED "showDate"
//COMPARE THE DATES IN A CONDITIONAL
if(todaysDate > showDate){
//SHOW DATE HAS PASSED, DELETE IT FROM DATABASE
results[i].destroy();
}else{
//SHOW DATE STILL VALID. APPEND ROW TO TABLE ALONG WITH A HEADER
$('tbody').append("<tr><th></th><th>Show Name</th><th>Location</th><th>Date</th><th>Time</th><th>People Needed</th><th>Cover</th></tr>");
$('#list').append("<tr id='row' class='Row'><td><figure id ='"+comicID+"'><img width='100' height='100' src='https://graph.facebook.com/"+comicID+"/picture'/><figcaption>"+comic+"</figcaption></figure></td><td id='showName'>"+ showName +"</td><td id='Location'>" + location + "</td><td id='Date'>" + date + "</td><td id='Time'>" + time + "</td><td id='Guests'>" + guests + "</td><td id='cover'>" + cover + "</td><td><a href='#' name ='" +showId+"' id='volunteer'>I'm Available</a></td></tr>");
}//closes else
}//closes for
//NOW CHECK THE NUMBER OF ROWS IN THE TABLE AND SEE IF IT IS EMPTY. IF SO, SHOW THE MESSAGE
var rowNum = $('#list tr:last').index() + 1;
rowNum = 0 ? $('#noShows').show() : console.log("number of rows:"+rowNum);
//WHEN THERE ARE NO ROWS THE CONSOLE WILL LOG "number of rows: 0", BUT IF THERE ARE ZERO ROWS THEN FIRST EXPRESSION OF THE TERNARY OPERATOR SHOULD WORK!!!!
}else{
//SHOWS MESSAGE IF THERE ARE NO RESULTS FROM THE DATABASE AT ALL
}