Show PivotTable [closed]

1

I'm creating a PivotTable from the values of an object, but at the time of generating the table it does not appear. Here is the code:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="batata.js"></script>
</head>
<body>
    <div id="tbldados"></div>
</body>
</html>
--------------------------------------------------------------------------------
var dados =[{
    "N1":" CONTROLE DE GESTÃO ORCAMENTARIA CONCESSÕES - INDICADORES / 2017 (MILHARES R$)",
    "N2":" ",
    "N3":"A1 - ATIVIDADE CORE",
    "ANO_ANTERIOR":1214363,
    "ANO_ATUAL":1335800,
    "VAR":121437,
    "JAN":2388000,
    "FEV":2268000,
    "MAR":1728000,
    "ABR":2424000,
    "MAI":1464000,
    "JUN":2124000,
    "JUL":1212000,
    "AGO":1356000,
    "SET":2400000,
    "OUT":1800000,
    "NOV":0,
    "DEZ":0,
    "TOTAL":456291000
}]


function createTable(obj){
    // Criar a table
    $('#tbldados').append('<table></table>'); // Adiciona a tabela ao body
    var table = $('#tbldados').children('table'); // Seleciona a tabela

    // Criar o head da table
    var thead =  "<tr>";
    for (coluna in obj[0]) {
        thead += "<th>" + coluna + "</th>";
    }
    thead += "</tr>";
    $("<table>").show();

    //Criar o body da table
    var tbody = "<tr>";
    obj.forEach(function(linha,) {
        for (item in linha) {
            tbody += "<td>" + linha[item] + "</td>";
        }
        tbody += "</tr>";
    })



    // Adiciona a tabela completa ao body
}
$(document).ready(function(){
    createTable(dados);
});
    
asked by anonymous 19.01.2018 / 17:57

1 answer

1

Your code has errors and no code to form the table. And you should use .append after completing the loops. After the loops, use .append by concatenating the results of the loops to insert the result in div .

One of the errors is:

$("<table>").show();

The <table> selector is invalid. It should be $("table").show(); . But .show() also has no function at all.

See the corrected code:

var dados =[{
    "N1":" CONTROLE DE GESTÃO ORCAMENTARIA CONCESSÕES - INDICADORES / 2017 (MILHARES R$)",
    "N2":" ",
    "N3":"A1 - ATIVIDADE CORE",
    "ANO_ANTERIOR":1214363,
    "ANO_ATUAL":1335800,
    "VAR":121437,
    "JAN":2388000,
    "FEV":2268000,
    "MAR":1728000,
    "ABR":2424000,
    "MAI":1464000,
    "JUN":2124000,
    "JUL":1212000,
    "AGO":1356000,
    "SET":2400000,
    "OUT":1800000,
    "NOV":0,
    "DEZ":0,
    "TOTAL":456291000
}];


function createTable(obj){
    // Criar a table
    $('#tbldados').append('<table></table>'); // Adiciona a tabela ao body
    var table = $('#tbldados').children('table'); // Seleciona a tabela

    // Criar o head da table
    var thead =  "<tr>";
    for (coluna in obj[0]) {
        thead += "<th>" + coluna + "</th>";
    }
    thead += "</tr>";

    //Criar o body da table
    var tbody = "<tr>";
    obj.forEach(function(linha,) {
        for (item in linha) {
            tbody += "<td>" + linha[item] + "</td>";
        }
        tbody += "</tr>";
    })

    // Adiciona a tabela completa ao body
    $("table").append(thead+tbody);
}
$(document).ready(function(){
    createTable(dados);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="tbldados"></div>
    
20.01.2018 / 01:37