当没有找到数据时设置为null

时间:2014-04-24 07:27:27

标签: php jquery

我使用jquery从php脚本中获取值并将它们放在标记之间。但有时没有任何东西作为价值发送。然后我想显示单词null。

$.ajax({
                            type: "POST",
                            url: "rapportage/rapport_detail", 
                            data: {"klant":klant,"project":project,"van":van,"tot":tot},
                            dataType: 'json',
                            error: function(){ alert("Onvoldoende gegevens beschikbaar om rapportage to genereren."); },
                            success: function(data){ // Plaats data op de juiste plek in de tabel 
                            var titel=data.titel;
                            projectid=data.projectid;
                            console.log(projectid);
                            $.post( "rapportage/rapport_detail", { projectid: "projectid"} );
                                projecttype =data.projecttype,
                                projectleider =data.projectleider,
                                projecttype =data.projecttype,
                                statusproject =data.statusproject,
                                startproject =data.startproject,
                                deadlineproject =data.deadlineproject,
                                omzetproject =data.omzetproject,
                                kostenproject=data.kostenproject,
                                totaalurenproject =data.totaalurenproject,
                                totaalminutenproject =data.totaalminutenproject,
                                urenkostenproject =data.urenkostenproject;
         var str='';
            for(var i=0,len=titel.length;i<len;i++){
               str+="<tr>"+"<td>" + projectid[i] +  "</td>";
               str+="<td>" + titel[i] + "</td>";
               str+="<td>" + projectleider[i] + "</td>";
               str+="<td>" + projecttype[i] + "</td>";
               str+="<td>" + statusproject[i] + "</td>";
               str+="<td>" + startproject[i] + "</td>";
               str+="<td>" + deadlineproject[i] + "</td>";
               str+="<td>" + "&#8364;" + omzetproject[i] + "</td>";
               str+="<td>" + "&#8364;" + kostenproject[i] + "</td>";
               var margeproject= omzetproject[i] - kostenproject[i];
               str+="<td>" + "&#8364;" + margeproject +  "</td>";
               str+="<td>" + totaalurenproject[i] + ":" + totaalminutenproject[i] + "</td>";
               str+="<td>" + "&#8364;" + urenkostenproject[i] + "</td>"+"</tr>";
               }
            alert(JSON.stringify(data));
            $("#table_1 tbody").append(str);
                                                    }
                                    });

但对于一个空变量,这是正确的做法吗?

1 个答案:

答案 0 :(得分:1)

这取决于,如果totaaluernproject为null或未定义,如果它为null则不需要测试只打印该值并且它将为null,否则如果它可以是未定义的,则可以执行此操作:

totaalurenproject[i] = totaalurenproject[i] || "null";
str+= "<td>" + totaalurenproject[i] + ":" + totaalurenproject[i];

这将在两种情况下为您提供更具控制状态的结果