I'm trying to retrieve a JSON that I've saved in localStorage, in the salvarOffline()
function. With code like the listarHorarios()
function I get a undefined
and the loop repetition does not work. My code:
function salvarOffline(data) {
window.localStorage.setItem("horariosOffline", JSON.stringify(data));
}
function listarHorarios() {
listaFormatada = JSON.parse(localStorage.getItem('horariosOffline'));
var tabelaHorarios = document.querySelector('.horarios');
for(var i = 0; i < listaFormatada.horarios.length; i++) {
var linhaHorario = document.createElement('tr');
linhaHorario.innerHTML =
'<td class="mdl-data-table__cell--non-numeric">' + listaFormatada[i].linha + '</td>' +
'<td class="mdl-data-table__cell--non-numeric">' + listaFormatada[i].partida + '</td>' +
'<td class="mdl-data-table__cell--non-numeric"><strong>' + listaFormatada[i].chegada + '</strong></td>';
tabelaHorarios.appendChild(linhaHorario);
}
}
The structure of my JSON is:
{
"horarios": [
{
"id": 1,
"linha": "Avenida",
"horario": {
"partida": "7:20",
"chegada": "7:40"
}
},
{
"id": 2,
"linha": "St. Antônio | Avenida | S. Sebastião",
"horario": {
"partida": "7:25",
"chegada": "8:00"
}
}
]
}