在Jquery Append中检查NULL

时间:2016-09-07 08:21:24

标签: php jquery

美好的一天。我正在尝试检查来自jquery append的值。这是我的Jquery

 success: function(resultData){
            $.each(resultData, function(key, val) { 
                no ++;
                    $("#result").append('<tr>  <td>' + no + '</td> <td> ' + val.FullName + 
                    '</td> <td> ' +val.attendancedate+ '  </td>'+
                    '<td>' + val.InTime + '</td>'+ 
                    '<td>' + val.OutTime + '</td>'+
                    '<td>' + val.total + '</td>' +
                    <?php if(($kode_tipe == "SPV_HO") or ($Nip == "1105321")){ ?> 
                    '<td> <a href="<?=base_url();?><?=$controller;?>/edit_jam/'+ val.nip+'/'+val.attendancedate+'/' +kode+'"> Edit </a> </td>' +
                     <?php } ?>
                    '</tr>');
            });
          }

所以。我想查看这部分

'<td>' + val.InTime + '</td>'+ 
'<td>' + val.OutTime + '</td>'+

所以逻辑如下:

if($InTime == NULL)
{
  echo "No Time";
}else{
  echo $InTime;
}

我真的不知道如何用jquery做到这一点。任何帮助将不胜感激。

这是resultData

[{"Nip":"1602744","FullName":"RAHMAT ","KodeDetailDinas":1,"KodeDinasHeader":"DN0000001","nip":"1602744"
,"attendancedate":"2016-09-14","dayname":"Wednesday","InTime":null,"OutTime":null,"week":38,"total":null
},{"Nip":"1602744","FullName":"RAHMAT ","KodeDetailDinas":1,"KodeDinasHeader":"DN0000001","nip":"1602744"
,"attendancedate":"2016-09-15","dayname":"Thursday","InTime":null,"OutTime":null,"week":38,"total":null
},{"Nip":"1602744","FullName":"RAHMAT ","KodeDetailDinas":1,"KodeDinasHeader":"DN0000001","nip":"1602744"
,"attendancedate":"2016-09-16","dayname":"Friday","InTime":null,"OutTime":null,"week":38,"total":null
},{"Nip":"1602744","FullName":"RAHMAT ","KodeDetailDinas":1,"KodeDinasHeader":"DN0000001","nip":"1602744"
,"attendancedate":"2016-09-17","dayname":"Saturday","InTime":null,"OutTime":null,"week":38,"total":null
},{"Nip":"1602744","FullName":"RAHMAT ","KodeDetailDinas":1,"KodeDinasHeader":"DN0000001","nip":"1602744"
,"attendancedate":"2016-09-18","dayname":"Sunday","InTime":null,"OutTime":null,"week":39,"total":null
},{"Nip":"1602744","FullName":"RAHMAT ","KodeDetailDinas":1,"KodeDinasHeader":"DN0000001","nip":"1602744"
,"attendancedate":"2016-09-19","dayname":"Monday","InTime":null,"OutTime":null,"week":39,"total":null
},{"Nip":"1602744","FullName":"RAHMAT ","KodeDetailDinas":1,"KodeDinasHeader":"DN0000001","nip":"1602744"
,"attendancedate":"2016-09-20","dayname":"Tuesday","InTime":null,"OutTime":null,"week":39,"total":null
},{"Nip":"1602744","FullName":"RAHMAT ","KodeDetailDinas":1,"KodeDinasHeader":"DN0000001","nip":"1602744"
,"attendancedate":"2016-09-21","dayname":"Wednesday","InTime":null,"OutTime":null,"week":39,"total":null

4 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var data = [{
  "Nip": "1602744",
  "FullName": "RAHMAT ",
  "KodeDetailDinas": 1,
  "KodeDinasHeader": "DN0000001",
  "nip": "1602744",
  "attendancedate": "2016-09-14",
  "dayname": "Wednesday",
  "InTime": null,
  "OutTime": null,
  "week": 38,
  "total": null
}, {
  "Nip": "1602744",
  "FullName": "RAHMAT ",
  "KodeDetailDinas": 1,
  "KodeDinasHeader": "DN0000001",
  "nip": "1602744",
  "attendancedate": "2016-09-15",
  "dayname": "Thursday",
  "InTime": null,
  "OutTime": null,
  "week": 38,
  "total": null
}, {
  "Nip": "1602744",
  "FullName": "RAHMAT ",
  "KodeDetailDinas": 1,
  "KodeDinasHeader": "DN0000001",
  "nip": "1602744",
  "attendancedate": "2016-09-16",
  "dayname": "Friday",
  "InTime": null,
  "OutTime": null,
  "week": 38,
  "total": null
}, {
  "Nip": "1602744",
  "FullName": "RAHMAT ",
  "KodeDetailDinas": 1,
  "KodeDinasHeader": "DN0000001",
  "nip": "1602744",
  "attendancedate": "2016-09-17",
  "dayname": "Saturday",
  "InTime": null,
  "OutTime": null,
  "week": 38,
  "total": null
}, {
  "Nip": "1602744",
  "FullName": "RAHMAT ",
  "KodeDetailDinas": 1,
  "KodeDinasHeader": "DN0000001",
  "nip": "1602744",
  "attendancedate": "2016-09-18",
  "dayname": "Sunday",
  "InTime": null,
  "OutTime": null,
  "week": 39,
  "total": null
}, {
  "Nip": "1602744",
  "FullName": "RAHMAT ",
  "KodeDetailDinas": 1,
  "KodeDinasHeader": "DN0000001",
  "nip": "1602744",
  "attendancedate": "2016-09-19",
  "dayname": "Monday",
  "InTime": null,
  "OutTime": null,
  "week": 39,
  "total": null
}, {
  "Nip": "1602744",
  "FullName": "RAHMAT ",
  "KodeDetailDinas": 1,
  "KodeDinasHeader": "DN0000001",
  "nip": "1602744",
  "attendancedate": "2016-09-20",
  "dayname": "Tuesday",
  "InTime": null,
  "OutTime": null,
  "week": 39,
  "total": null
}, {
  "Nip": "1602744",
  "FullName": "RAHMAT ",
  "KodeDetailDinas": 1,
  "KodeDinasHeader": "DN0000001",
  "nip": "1602744",
  "attendancedate": "2016-09-21",
  "dayname": "Wednesday",
  "InTime": null,
  "OutTime": null,
  "week": 39,
  "total": null
}]

$.each(data, function(key, val) {


val.InTime !=null ? console.log(val.InTime) : console.log("")





});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

使用!= null 如需进一步说明,请访问this

答案 1 :(得分:1)

success: function(resultData){
        $.each(resultData, function(key, val) { 
            no ++;
            var inTime = (!val.InTime) ? 'No Time' : val.InTime;

                $("#result").append('<tr>  <td>' + no + '</td> <td> ' + val.FullName + 
                '</td> <td> ' +val.attendancedate+ '  </td>'+
                '<td>' + inTime + '</td>'+ 
                '<td>' + val.OutTime + '</td>'+
                '<td>' + val.total + '</td>' +
                <?php if(($kode_tipe == "SPV_HO") or ($Nip == "1105321")){ ?> 
                '<td> <a href="<?=base_url();?><?=$controller;?>/edit_jam/'+ val.nip+'/'+val.attendancedate+'/' +kode+'"> Edit </a> </td>' +
                 <?php } ?>
                '</tr>');
        });
      }

您的代码可以使用一些重构。

答案 2 :(得分:0)

你走了:

if (val.InTime.length > 0){
  $("#result").text(InTime);
} else {
  $("#result").text("No Time");
}

答案 3 :(得分:0)

从我的观点来看,你的渲染逻辑非常复杂,而不是编写完整的html渲染代码,试试这个:

$.each(resultdata, function(val,index){
   var tr = $('<tr>');
   tr.append($('<td>',{
       'text': "no"
   }));
   tr.append($('<td>',{
       'text': val.FullName
   }));
   tr.append($('<td>',{
       'text': val.attendancedate
   }));
   if(val.InTime === "NULL"){
      tr.append($('<td>',{
         'text': "No Time"
      }));
   }else {
       tr.append($('<td>',{
         'text': val.InTime
      }));
   }
   //Do the same for out time as well
   $("#result").append(tr);
});

试试这个。