Generating PivotTable with Jquery

0

I have the following problem, I'm doing a search field, to search by the name of the client, so far so good, I was able to bring the clients but they are in an array, my problem is how I divide each client into separate line

I want to do this:

Butit'scominglikethis:

Itriedtousethe$.eachthreaded,butitendsupduplicatingtheregistry,ifitgetsonlyoneoftheregistryright,butthenamehasitinthesamelineasitsIDandAGE

success:function(data,status){varvaloresID;vararrID=[];vararrNome=[];vararrIdade=[];for(vari=0;i<data.d.length;i++){varitem=JSON.stringify(data.d[i]["ID"]);

                    arrID.push(data.d[i]["ID"]);
                    arrNome.push(data.d[i]["Nome"]);
                    arrIdade.push(data.d[i]["Idade"]);


                }
                $('.child').remove()
                $('.valorTr').after('<tr class="child"><td>'+JSON.stringify(arrID)+'</td><td>'+arrNome+'</td><td>'+arrIdade+'</td></tr>');

            }
    
asked by anonymous 16.04.2018 / 20:19

1 answer

2

This happens because you are using JSON.stringify that turns your array into a string.

To solve your problem you can do the following:

for (i =0; i < arrID.length; i++){
 $('.valorTr').after('<tr class="child"><td>'+arrID[i]+'</td><td>'+arrNome[i]+'</td><td>'+arrIdade[i]+'</td></tr>');
}

A better solution would be to work only with an array containing the object you retrieve:

dadosTabela = []

// código omitido (loop dos dados)
dadosTabela.push(dados = {
  "Id": 1,
  "Nome": "Laerte",
  "Idade": 23
})
dadosTabela.push(dados = {
  "Id": 2,
  "Nome": "João",
  "Idade": 18
})

dadosTabela.forEach(x => console.log(x['Id'] + '\t' + x['Nome'] + '\t' + x['Idade']))
    
16.04.2018 / 20:31