dimanche 28 juillet 2019

How to handle the if statement inside the each of ajax?

I am trying to load some JSON data from back-end and output them as the html table. With no luck, I got confused how to set if-else conditions inside the ajax each.

There are 3 types of status from my data list. So I was trying to show each of them a different button by that.

However, always get wrong results when I do ELSE. even if it looks fine! the code make no sense at all...

And doesn't "else if" exist?

$.each(data.list, function (i, item) {
                var giftCenter = 
                    '<tr>'+
                        '<td width="20%">'+ item.sn +'</td>'+
                        '<td width="20%">'+ item.create_time +'</td>'+
                        '<td width="20%" width="20%">'+ item.out_nick_name +'</td>'+
                        '<td><img class="gem" src="img/gem.png" alt="" /> '+ $numberFormat.format(item.amt) +'</th>'+
                        '<td width="20%" class="gift-operate">'+ item.status_name +'<button type="button" class="acceptBtn">YES</button><button type="button" class="cancelBtn">NO</button><button type="button" class="confirmBtn">CONFIRM</button></td>'+
                    '</tr>';
                $(".playGiftTab2 tbody").append(giftCenter);
                // none of these will show
                if(item.status == "2" || item.status == "-1" || item.status == "-2" || item.status == "-3"){
                    $(".acceptBtn, .cancelBtn, .confirmBtn").hide();
                }
                // only if 0 1
                else if(item.status == "0" || item.status == "1"){
                    $(".acceptBtn, .cancelBtn, .confirmBtn").show();
                    // receiver
                    if(item.is_giver == 0){
                            $(".confirmBtn").hide();
                            $(".acceptBtn").hide();
                            $(".cancelBtn").hide();
                    }
                    // giver
                    else if(item.is_giver == 1){
                        // status 0
                        if(item.status == "0"){
                            $(".confirmBtn").hide();
                            $(".acceptBtn").show();
                            $(".cancelBtn").show();
                        }
                        // status 1
                        else if(item.status == "1"){
                            $(".acceptBtn").hide();
                            $(".cancelBtn").hide();
                            $(".confirmBtn").show();
                        }   
                    }
                }
            });

Aucun commentaire:

Enregistrer un commentaire