2

After appending a row to a table on the HTML, How to take all the data (in JSON format) and store in local storage?

This is my JS code:

// Our next step is to check if we have something on local storage already saved 
// in our machine and if we do we need to place that in the page

    $("#div1").hide();
if(localStorage.getItem('dataToStore')) {

//String to JSON object
var exidata= JSON.parse(localStorage.getItem('dataToStore'));

$('#details tr:last').after('<tr>');
$.each(exidata, function(key, value){
    $('#details tr:last').append('<td>'+value+'</td>');
});
$('#details tr:last').after('</tr>');
} else{
alert('Problem mate, its empty!!');
}   

});

Upon loading the page it checks if there is anything in localstorage, if so it prints it out in a table. The next step would be to switch over to the registration form, fill in the details and then click submit, which then adds the details to the table. So you now have n+1 rows. I then want to take all the data (JSON format)and store it in the local storage.

Apologies if the problem/question is not clear.

Many thanks

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
  • Is [this](http://stackoverflow.com/a/2010948/2366976) what you are looking for? – Tobias Roland Nov 18 '13 at 03:57
  • @Tobias, unfortunately that is not what I am looking for. I understand I would need .stringfy & .parse to store JSON Object in local storage. But the problem I face is taking all the data from the table and trying to store that. – user3003259 Nov 18 '13 at 04:05
  • Why do you need the table in JSON format if you are not sending it over the wire? You can easily store it as the innerHTML like localStorage.tablestuff = $('#table').html(); – King Friday Nov 18 '13 at 04:33
  • Hi Jason, the instructions given require that I store the data in JSON format. Instructions: 1. Store the data in session cache in json format – HTML5 feature 2. Read the data from session cache and display in table format. 3. Table Header to have grey color 4. Alternate table rows to have in different color. – user3003259 Nov 18 '13 at 04:52
  • 1
    Seems you've got the reading & displaying part worked out. And, you only need help saving the data from the table back in to the localStorage in JSON format. Consider using [table-to-json](http://lightswitch05.github.io/table-to-json/) for the same – Vikram Deshmukh Nov 18 '13 at 10:46
  • Hi Srvikram13, thanks for that, i've looked at the example, seems like it will be of good use! – user3003259 Nov 19 '13 at 06:56

0 Answers0