Creating an HTML table to showcase array data stored in a session

After successfully storing data from an HTML table on page 1 in an array object (arrData) and saving it to session storage, the next challenge is how to display this data on a new HTML table on page 2. I am new to JavaScript, so any help would be appreciated. Thanks in advance!

https://i.sstatic.net/MIWkf.png

JAVASCRIPT CODE ON PAGE 1

var arrData = [];               
$("#checkout").on('click',function(){

$("#table tr").each(function(){
    var currentRow = $(this);

    var col1_value = currentRow.find("td:eq(0)").text();
    var col2_value = currentRow.find("td:eq(1)").text();
    var obj = {};

    obj.col1 = col1_value;
    obj.col2 = col2_value;

    arrData.push(obj);
   sessionStorage.myArrData = JSON.stringify(arrData);
 });
console.log(arrData);

});

HTML TABLE TEMPLATE ON PAGE 2

<table class="table table-checkout" id="table">
<thead>
  <tr>
    <th>Item</th>
    <th>Quantity</th>
  </tr>
</thead>
<tbody>
</tbody>

JAVASCRIPT CODE ON PAGE 2

var arrData = JSON.parse(sessionStorage.myArrData);

Answer №1

It is essential to utilize sessionStorage.setItem("foo", 12) instead of sessionStorage.foo = 12;

Using the latter method adds a new property to the JavaScript object, rather than interacting with the browser session API. Consequently, when the page reloads, the attached object will no longer be accessible.

To retrieve the item, utilize sessionStorage.getItem

Explore Mozilla's documentation on sessionStorage for details on setItem and getItem methods

After implementing the storage mechanism, you'll need a technique for dynamically adding table rows within the table. While there are various frameworks available for this purpose, it is also possible to manually construct tables (albeit requiring more steps compared to other elements)

Learn how to insert row into an HTML table body using Javascript

Answer №2

From what I gather from the information above, it seems that you have data stored in an array of objects like this after executing

var arrData = JSON.parse(sessionStorage.myArrData);
:

Data structure of arrData:

[
{col1:"Item1", col2:"quantity1"},
{col1:"Item1", col2:"quantity1"},
...
]

Now, if you want to display this data on Page 2, you can use the following code snippet:

var rows = "";
arrData.map((row)=>{
   var rowHTML = '<tr><td>'+row.col1+'</td><td>'+row.col2+'</td></tr>';
   rows = rows+rowHTML;
})
var tbodyElement = document.queryselector('#table tbody');
tbodyElement.innerHTML = rows;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In a REST api, what is the appropriate response for a property that is missing a value?

Is it better for a property with no value assigned to be returned as null, or should the REST API skip such properties entirely? Let's consider a user object example with first_name and last_name. (In the below example, last_name is not necessarily a ...

Separating an element and adding it to a div without duplicating

Is there a way to detach or clone the a tag in my HTML without changing the structure? I'm using a page builder and duplicating the a tag when I have multiple elements, resulting in unwanted behavior. Current HTML: <div class="wrapper"> &l ...

Navigating and Organizing in Ionic Service Factory

Apologies for the beginner question. I am looking to incorporate filtering and sorting by name on my webpage. However, I have encountered two issues after attempting to implement this functionality using a factory in services.js: When typing a search ter ...

Sending information from Vue to Express for processing

Is there a way to pass data from Vue to an express server? For example, in the scenario below, I am looking to send the data logged in my Vue function to the "id" variable on the server side. expressApp.post('/delete' , function (request, respons ...

Declare a jQuery variable containing HTML elements

Greetings! I am facing an issue while attempting to create a jQuery variable that stores a table meant for displaying in different sections of a website. Unfortunately, I am encountering an error and struggling to comprehend the reason behind it. Below is ...

Is it possible to test code that utilizes a different framework from jQuery using QUnit?

Currently, I am in the process of developing a compact library that is capable of utilizing multiple frameworks (specifically jQuery, Prototype, YUI2). To ensure its functionality, I am conducting tests using QUnit. Nevertheless, one setback is that QUnit ...

An in-depth guide on implementing Highcharts.Tooltip functionality in a TypeScript environment

Currently, I am trying to implement a tooltip activation on click event in Highcharts by following an example from this URL: Highcharts - Show tooltip on points click instead mouseover. The challenge I'm facing is that I am using TypeScript and strugg ...

Having issues with altering the CSS property in JavaScript

Here is a snippet of PHP code I am working with: <div class="log-in"> <span>Hello '.@$_SESSION['user'].' !</span> <div id="img" onc ...

Preventing non-breaking spaces and characters from being added in Jquery Mobile

When developing an application using jQuery Mobile, I encountered a strange issue on my homepage. In between the listview, this code snippet appeared: &nbsp;&nbsp;&nbsp;&nbsp; But without the following meta tag: <meta http-equiv="Cont ...

Encountering issues while trying to install Java using NPM

Encountering errors when attempting to run the following command to install java dependencies for NPM. NPM install -g java In need of assistance to fix this error. C:\WINDOWS\system32>npm i -g java [email protected] install C:\D ...

Create a timestamp with Javascript rendering

Looking to convert a Unix timestamp into a human-readable format without adjusting for my browser's timezone. For example, if the timestamp is 1400167800 (05 / 15 / 14 @ 3:30:00pm UTC) and my timezone is +2, how can I display this timestamp as ' ...

How can I ensure my AngularJS Controller variable is accessible across all page contexts?

Working with AngularJS, I have a view controller where I initialize a variable called recipesData. Here is the code for the controller: (function() { 'use strict'; angular .module('myApp') .controller('Coo ...

tips for accessing variables within app.get

Is there a way to make a variable or a set of variables inside app.get accessible throughout the entire project? I am working on capturing information from an SMS text message, organizing it into the "messageData" variable, and then sending it to the "Mess ...

Multi-file upload PHP form

I have successfully implemented a contact form with file upload functionality in my code. However, I am facing an issue in adapting it for multiple file uploads. Below is the structure of the form: <?php <form id="formulario" name="formulario" ...

Definition of Redux store state object

Recently, I started using Typescript in a React/Redux project, and I am facing some confusion regarding how type definitions should be incorporated into the application state. My goal is to make a specific piece of state accessible to a container component ...

I am having trouble placing the button within the image

Essentially, my goal is to place the button within the image and ensure it remains in its position when transitioning to mobile mode or adjusting window size. This is the desired outcome: https://example.com/image https://example.com/button-image .img ...

Increasing the opacity of a background image when a new div is added

While working on my chatbot assignment, I set the background image opacity to 0.02. Unexpectedly, as I input more messages, the opacity gradually increases all the way to 1. Although this effect is pretty neat, it wasn't my original intention. Some ...

"Having trouble with plUpload only able to upload a single file to the server in HTML

My current code is functioning well with HTML5 and Silverlight, but I'm experiencing issues with Flash and HTML4. The upload form seems to stall and only the first file gets uploaded to the directory. Below is the JavaScript code for one of the uploa ...

Issues detected between Angular and Express rendering operations

Having an issue with my angular code. It runs perfectly on its own, but when I try to access it on localhost with express, it only displays the HTML file. Here's my server code: var express = require('express'), app = express(); app ...

Is there a way to run a Javascript function only after an onload event has occurred and returned a specific value?

This question may be a bit lengthy, so I'll do my best to provide an explanation. There are two JavaScript functions that need to execute after the page loads - let's call them function1() and function2(). function1() uses AJAX to fetch data fro ...