Rene
Rene

Reputation: 13567

when there is no data found set null

I'm using jquery to get values from an php script and place them between tags. But sometimes nothing is send as an value. Then i would like to show the words 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);
                                                    }
                                    });

But is this the right thing to do for an empty variable?

Upvotes: 0

Views: 70

Answers (1)

Labib Ismaiel
Labib Ismaiel

Reputation: 1340

it depends, if totaaluernproject is null or undefined, if it is null then there is no need to test just print the value and it will be null, other wise if it can be undefined, you can do this:

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

this will give you the result in both cases with a more controlled state

Upvotes: 1

Related Questions