Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing array content in session storage [duplicate]

I have data being displayed in a table. I delete a row, I need to hide it until that deletion is also exposed to the backend (It is exposed only after a minute). There is also auto-refresh that happens every 25 seconds, which brings the stale data (only after a minute, updated data is available to the backend).

I decided to use sessionStorage to store the deleted objects and then whenever the stale data comes I compare and not show in the table.

But sessionStorage doesn't support array. So when user deletes one object, goes to some other page, comes back and deletes another object (sessionStorage variable is overwritten) and then refreshes, only the last object deleted is hidden, all other deleted objects are shown

I am not sure how to store the deleted objects in session storage.

like image 736
user2761431 Avatar asked Jul 01 '18 12:07

user2761431


People also ask

Can I store an array in session storage?

Saving arrays to localStorage and sessionStorageWe can now save it to localStorage or sessionStorage using the setItem() method: localStorage. setItem("ourarraykey",JSON. stringify(ourArray));

How do you set an array of objects in session storage?

My code is: var parsedResult = JSON. parse(result); // where result is the above JSON if (parsedResult. Status === "OK!") { // Set sessionStorage vars if (typeof(Storage) !==

What should be stored in session storage?

Session storage is a popular choice when it comes to storing data on a browser. It enables developers to save and retrieve different values. Unlike local storage, session storage only keeps data for a particular session. The data is cleared once the user closes the browser window.

Can I store array in session PHP?

Yes, PHP supports arrays as session variables.


1 Answers

If you store array of items its pretty easy. You can store an array using json stringify:

sessionStorage.setItem('deletedItems', JSON.stringify(array))

Then retrieve it like this:

JSON.parse(sessionStorage.getItem('deletedItems'))

Before storing next deleted item you can then retrieve previous items in storage, push to existing array the new item and store it back

like image 148
belicam Avatar answered Sep 30 '22 02:09

belicam