Table showing undefined that does not exist

2

I have a javascript generate a PivotTable. Looking at Stack Overflow I looked for methods to print a report from this table. In my case, when the user clicks Imprimir tabela opens a page with the layout of the report table, my code makes the whole report correct, and so, but I do not know why this blessed undefined appears: / p>

Partoftableimpressioncode:

functionrelatorio(){varconteudo;varlinhas;vardataFinalD2=newDate();varn=0;for(i=0;i<ContPosic;i++){//AdicionandoprimeiralinhavarRlservice=$('.Rlservice'+(i+1)).val();varRlsistema=$('.Rlsistema'+(i+1)).val();varTpf=$('.Tpf'+(i+1)).val();varProdut=$('.Produt'+(i+1)).val()varAntPraz=$('#AntPraz'+(i+1)+'option:selected').text();varQtdUteis=$('.QtdUteis'+(i+1)).text();varDtIniRes=$('.DtIniRes'+(i+1)).text();varQtdDiaCo=$('.QtdDiaCo'+(i+1)).text();varDtFinall=$('.DtFinall'+(i+1)).text();varQtdGames=$('.QtdGames'+(i+1)).text();varPfGamess=$('.PfGamess'+(i+1)).text();vardata;if(i==0){vardia=parseInt(DtIniRes.substring(0,2));varmes=parseInt(DtIniRes.substring(3,5));varano=parseInt(DtIniRes.substring((DtIniRes.length-4),(DtIniRes.length-0)));data=newDate();data.setDate(dia);data.setMonth((mes-1));data.setFullYear(ano);}if(n==1){data=dataFinalD2;}if(n>=1){data.setDate(data.getDate()+1);}vardatainicial=('0'+data.getDate()).slice(-2)+'/'+('0'+(data.getMonth()+1)).slice(-2)+'/'+data.getFullYear();//setandoprégame//datainicialdata.setDate(data.getDate()+21-1);vardatafinal=('0'+data.getDate()).slice(-2)+'/'+('0'+(data.getMonth()+1)).slice(-2)+'/'+data.getFullYear();linhas+='<tr>'+'<td>'+Rlsistema+'</td>'+'<td>Pré-game</td>'+'<td>'+datainicial+'</td>'+'<td>'+datafinal+'</td>'+'</tr>';//fordosgamesvarx=Math.round(QtdGames);for(j=0;j<x.toFixed(1);j++){vardatainicial1;vardatafinal1;//#######################################################if(i>=1){//calculosdadataparamaisde1item...dataFinalD2.setDate(dataFinalD2.getDate()+1);datainicial1=('0'+dataFinalD2.getDate()).slice(-2)+'/'+('0'+(dataFinalD2.getMonth()+1)).slice(-2)+'/'+dataFinalD2.getFullYear();//setandoprégamedataFinalD2.setDate(dataFinalD2.getDate()+14-1);datafinal1=('0'+dataFinalD2.getDate()).slice(-2)+'/'+('0'+(dataFinalD2.getMonth()+1)).slice(-2)+'/'+dataFinalD2.getFullYear();//#######################################################}else{//calculosdadataparaapenasumalinhadata.setDate(data.getDate()+1);datainicial1=('0'+data.getDate()).slice(-2)+'/'+('0'+(data.getMonth()+1)).slice(-2)+'/'+data.getFullYear();//setandoprégamedata.setDate(data.getDate()+14-1);datafinal1=('0'+data.getDate()).slice(-2)+'/'+('0'+(data.getMonth()+1)).slice(-2)+'/'+data.getFullYear();}//#######################################################linhas+='<tr>'+'<td></td>'+'<td>Game'+(j+1)+'</td>'+'<td>'+datainicial1+'</td>'+'<td>'+datafinal1+'</td>'+'</tr>'}//datafimdoprégamedata.setDate(data.getDate()+1);vardatainicial2=('0'+data.getDate()).slice(-2)+'/'+('0'+(data.getMonth()+1)).slice(-2)+'/'+data.getFullYear();//setandoprégamedata.setDate(data.getDate()+14-1);vardatafinal2=('0'+data.getDate()).slice(-2)+'/'+('0'+(data.getMonth()+1)).slice(-2)+'/'+data.getFullYear();if(j>=x){if(n==0){dataFinalD2=data;}n++;}linhas+='<tr>'+'<td></td>'+'<td>Post-Game</td>'+'<td>'+datainicial2+'</td>'+'<td>'+datafinal2+'</td>'+'</tr>';}//tabela+linhasqueforamadicionadasacimavartable='<table>'+'<thead>'+'<tr>'+'<td>ReleaseSistema</td>'+'<td>Iteração</td>'+'<td>DatadeInício</td>'+'<td>DatadeFim</td>'+'</tr>'+'</thead>'+'<tbody>'+linhas+'</tbody>'+'</table>';conteudo="<html> <head>"
    +"<title>BANCO XXXXXXX- Relatório</title>"
    +"<style> table {"
    +"border-collapse: collapse;"
    +"} "
    +"table, th, td {"
    +"border: 1px solid black; padding: 4px;"
    +"} </style>"
    +"</head>"
    +"<body><header>"
    +"</header><main>"
    +''
    + table
    +"</main></body><html>";

  tela_impressao = window.open('about:blank');
  tela_impressao.document.write(conteudo);
  tela_impressao.window.print();
  // tela_impressao.window.close();
}
    
asked by anonymous 17.07.2017 / 18:23

3 answers

1

After analyzing my code, I saw that the error was a simple matter of initializing the variables that used to be this way

var conteudo; 
var linhas;

And now I put it like this:

var conteudo = ""; 
var linhas = "";

undefined was appearing because when I added the html in the content or the rows of the table (tr) in the rows variable I left what was before and added what I wanted, ie without the variables being properly initialized they came with the value pre-defined as undefined, initializing them I did not have this problem anymore.

    
19.07.2017 / 16:14
2

The return of document.write is always undefined . When you call the print method, you're going to render everything, including that.

Just use:

tela_impressao.window.print(conteudo);

Q.: Mounting HTML in this way exposes your code to vulnerabilities. For a study this may be OK, but forward you'd rather have a ready-made HTML template file, in which you can just enter the information you need with something like PHP, Java, .NET (ie: C #) or even Angular.

    
17.07.2017 / 18:46
-1

I was not able to post a functional code using the stackoverflow tool so I created the example jsfiddle. Any remaining questions you can post in the comment below that will help you, get the code and modify it.

EXAMPLE

    
17.07.2017 / 23:25