DEV Community

Njeri Cooper
Njeri Cooper

Posted on

Storing Local Data with Javascript

I'm trying to store a table's data locally. I'm a bit confused about where I should store the cells; in an array? Will the array return each cell in the correct row index? How should I set the local data variable if I'm passing rows and a table as the arguments?

In my code, the table creates a new row from user input. When I refresh, the table goes away. I'd like to keep the table from the previous session.

Here's what I have so far:
(press the play button to run the code)

Latest comments (4)

Collapse
 
gauravumrani profile image
Gaurav • Edited

Hi, You can store the data in local storage, and if you want the data to be in order, you can do this

  1. Whenever new data is created, store the id also, it should be increment, ex- Date.now().getTime()
  2. Store the data in local storage
  3. The data should be stored in an Array

Example data format =

var data = [{
      id: 1,
      more: data
    }, {
      id: 2,
      more: data
 }];

Now when the user refreshes the page, you can take the data from local storage, sort it by id and display it

Collapse
 
njericooper profile image
Njeri Cooper

Interesting. Thank you!

Collapse
 
likejean profile image
likejean • Edited

Storing data in a declared array won't reserve your data in that array after you recompile the code again. If you don't involve any backend storage here and only looking for client-side storage, you would have to use some kind of browser-built Javascript APIs in order to save your data. The simplest and most widely supported is WebStorage where you have persistent storage (localStorage) or session based (sessionStorage). The Google has developed its own storage. There are plenty tutorials available how to store data locally.

Collapse
 
njericooper profile image
Njeri Cooper

Thanks! I'll look that up.