vendredi 26 mai 2017

$.isEmptyObject not working in an if statement

I'm having trouble creating an if statement with $.isEmptyObject.

Here is my whole js file:

$("#tabell").hide();

$( '#sok-button').on('click', function () {
$("tbody").empty();
        var ord = $('#livsmedelsSokOrd').val();

 // Search happens through webservice  

          $.ajax({
            url: "http://ift.tt/2qmmzkn",
            dataType: "jsonp",
            jsonp: "getLivsMedel",
            data: {
              callback: 'getLivsMedel',
              namn: ord
            }
          });

 });


function getLivsMedel(response) {

 // Problem starts here   
    if(!$.isEmptyObject(response)){

    var livsmedel = response.livsmedel;

        livsmedel.forEach(function (produkt) {
            $('#tabell > tbody').append('<tr>' + '<td>' + produkt.namn + '</td>' + '<td>' + produkt.energi + '</td>' + '<td>' + produkt.kolhydrater + '</td>'
+ '<td>' + produkt.protein + '</td>' + '<td>' + produkt.fett + '</td>' + '</tr>');
        });

        $("#tabell").show();
   }

   else {
        $("tbody").empty(); //I've also tried with $("tbody").hide(); and $("#tabell).hide();
   }
 }

So the basic idea is that user does a search in an input field, then presses button. After that the results come in a table. The code works mostly fine but the last else doesn't work. So the table doesn't go away when there aren't any search results. And I would like it to be hidden when there are no results to show. The else doesn't work at all, not even when I try to alert something.

So I guess the problem is somewhere in if(!$.isEmptyObject(response)). Console doesn't show any errors/warnings though. Does someone know what's wrong with it?

Aucun commentaire:

Enregistrer un commentaire