Question

I'm trying to do a few things with some rows of data I pull from a database. The first thing I have to do is compare the date the row was saved to todays date, if the date the row was saved has passed, I need to delete it while appending the relevant dates to a table. When the for loop that iterates over the database rows is finished, I need to then do a check of the number of rows in the table. If it is empty because there were only past date rows and they've all been deleted, I'll need to show the user a message explaining the empty table, otherwise I don't need to show anything. This is where I'm trying to use the combination of the ternary operator and the jquery method of getting the num of table rows. For some reason, no matter if the number of rows is zero or more, the second expression of the ternary operator kicks in. So even though the number of rows is zero, the code acts as if there are rows present and won't show the message intended for an empty table. I've omitted some code for readability.

//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

    }
Was it helpful?

Solution

your line:

rowNum = 0 ? $('#noShows').show() : console.log("number of rows:"+rowNum);

is not correct, there are a few problems with it. the first problem i see is rowNum = 0 is an assignment, not a boolean expression.

you should just use an if statement:

if(rowNum==0)
{
    $('#noShows').show();
}
else
{
    console.log("number of rows:"+rowNum);
}

more infomation can be found looking at the answer to using ternary operator in javascript to invoke two functions?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top