I got this project for school, I need to load data from sessionStorage into an html table when my .html loads (not just from the cache).
I am inserting new data into sessionStorage from another .html, I need to save it dynamically (for example, the first row of my table is rfc0, razon_social0, direccion_fiscal0, the next row is rfc1, razon_social1, direccion_fiscal1, etc.), I use this Javascript function to save my data:
function getInfo() { if (sessionStorage.contador){ var i = parseInt(sessionStorage.contador); } else{ sessionStorage.setItem('contador', "0"); var i = parseInt(sessionStorage.contador); } var rfc = document.getElementById('RFC').value.toUpperCase(); sessionStorage.setItem('rfc' + i, rfc); var razon_social = document.getElementById('razon_social').value; sessionStorage.setItem('razon_social' + i, razon_social); var domicilio_fiscal = document.getElementById('domicilio_fiscal').value; sessionStorage.setItem('domicilio_fiscal' + i, domicilio_fiscal); var banco = document.getElementById('banco').value; sessionStorage.setItem('banco' + i, banco); var numero_cuenta = document.getElementById('numero_cuenta').value; sessionStorage.setItem('numero_cuenta' + i, numero_cuenta); i++; sessionStorage.setItem('contador', i); }
In my onpageshow body, I use this function to check storage support and call data:
function checkStorage() { if(typeof(Storage) !== "undefined") { if(sessionStorage.flag) { agregarDatos(); } } else { alert('Sorry! No Web Storage support...') } }
My agregarDatos () function:
function agregarDatos() { var table = document.getElementById("tabla_clientes"); var i = parseInt(sessionStorage.contador); var conta = 0; while(conta <= i){ var row = table.insertRow(-1); var cell0 = row.insertCell(0) var cell1 = row.insertCell(1); var cell2 = row.insertCell(2); var cell3 = row.insertCell(3); var cell4 = row.insertCell(4); cell0.innerHTML = '<input class="check" type="checkbox" onClick="validateButton()">'; cell1.innerHTML = sessionStorage.getItem('rfc' + conta); cell2.innerHTML = sessionStorage.getItem('razon_social' + conta); cell3.innerHTML = sessionStorage.getItem('domicilio_fiscal' + conta); cell4.innerHTML = '<input class="editar" type="button" value="Editar">'; conta++; } }
When I check this, I get nothing, even when I add an item the first time, I canโt understand what is wrong.
If I save the data without ID, I can add only one row with the latest data saved.
PD Only I can use Javascript ...