Step by step guide to showcasing images dynamically in user interface

My current project involves displaying a screen with an HTML table and an image. The HTML table is fully dynamic.

The Code Working Process

When the user loads a page (with a URL), I render an HTML table in different parts as the page loads. I retrieve all the table data in JSON format at once and display 3 rows at a time on the UI, with an interval of 3 seconds between each set. Once the full table is loaded, I show an image for some time before loading the table again and displaying another image. Everything works fine, but now I want to dynamically display multiple images.

What I'm Trying to Achieve

Previously, I only had one static image to display using

<img src="Image/Counter A/CounterA1.jpg" alt="Some Image" width="460" height="345">
, but now "Counter A" has multiple images. For example, there could be two or three images. So, what I want to do is load the table first and then sequentially display the images after each table reload - showing the first image, reloading the table, and then showing the second image.

The Working Process

Here is the sequence: Table loading -> displaying 3 rows every 3 seconds until the end -> showing Image1 (CounterA1.jpg) -> reloading the table -> showing Image2(CounterA2.jpg) -> repeating the cycle of table reload and image display.

I have already successfully displayed the HTML table and one static image. Now, I aim to achieve this dynamically.

// JavaScript code snippet
var tableValue = [{
    // Table value objects here
}];

// Function to initialize the table
function initTable(tableValue) {
    addTable(tableValue)
    interval = window.setInterval(showRows, 3000); 
}

// Function to handle hiding the image and showing the table
function hideImage() {
    $("#displayImage").show(); 
    $("#DisplayTable").hide();

    setTimeout(function() {
        initTable(tableValue);
    }, 3000);
}

// Rest of the functions are similar for handling table rows and image display

// Initial call to start the process
initTable(tableValue);

// CSS styles for the table
tbody>tr>td {
    // Styles for table cells
}

// More CSS styles for table cell colors, widths, etc.

.hidden,
.already-shown {
    display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div id="DisplayTable"></div>
<div id="displayImage" style="display: none">
</div>

In order to dynamically display multiple images based on the counter, I have stored the image information in an object like this:

var images = {"Counter A":["CounterA1.jpg", "CounterA2.jpg"]}
.

Edit:

I have added the following code snippet to address this issue:

// Further JavaScript code for iterating through image array and displaying the images
var images = {"Counter A":["CounterA1.jpg","CounterA2.jpg"]}; 

for (var key in images) {
    var imageList = images[key];
    console.log(imageList.length)
    
    for (i = 0; i < imageList.length; i++) {
        console.log(imageList[i])
        var img = $('<img />').attr({
            'src': 'Image/'+key+'/'+imageList[i], 
            'alt': 'Some Image',
            'width': '90%',
            'height': 680
        }).appendTo('#displayImage'); 
    }
}

I have made progress towards achieving my goal, but the issue I currently face is that the images are rendering one below the other instead of alternating between them. Each table should be followed by the corresponding image, but they are currently stacked vertically. Please review the provided snippets for more clarity.

Here is how the current image rendering looks: https://i.stack.imgur.com/9syTo.png

And another image reference: https://i.stack.imgur.com/o7Pm2.png

Additional image reference: https://i.stack.imgur.com/osghJ.png

Answer №1

Give this a try.

I recently implemented a new function to format images in HTML. After determining the length of the image array, I introduced a variable called cnt (short for count) and set it to increment with each loop iteration. Using modulo operations, I was able to cycle through the images effectively.

var imgLen = 0;
var cnt = 0;

var tableValue = [{
    "Item Name": "MANCHOW V SOUP",
    "SellingPrice": 100
}, {
    "Item Name": "SMIRNOFF GREEN APPLE 60",
    "SellingPrice": 202
}, 
...
// Further code here

function initTable(tableValue) {
    addTable(tableValue)
    showRows();
    interval = window.setInterval(showRows, 1000); // setting an interval to display the table gradually
}
// Remaining functions and code snippets...
tbody>tr>td {
    white-space: normal;
    border-collapse: collapse;
    font-family: Verdana;
    font-weight: bold;
    font-size: .9em;
}
// CSS styles continue...
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div id="DisplayTable"></div>
<div id="displayImage" style="display:none">

</div>

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

"Implementing a Modular CSS Approach and Uniform Styling Across Your App

How can we handle the scenario of implementing specific styling for h1 tags within a modular CSS approach? In the past, this would have been achieved through inheritance in a single stylesheet. For example: .h1 { font-size: 3rem; /* more styles */ } .h2 { ...

Preparing JSON data for creating a wind map with Leaflet

I am currently working on converting netCDF data to JSON in order to use it with leaflet-velocity. This tool follows the same format as the output of grib2json used by cambecc in earth. An example of sample JSON data can be found at wind-global.json. By u ...

Passing data from a Vue.js component to a router in index.js

I am attempting to transmit a JWT token value from the component Login.vue and verify it in the router/index.js before directing the user to the next page. Login.vue: <script> import axios from "axios"; export default { name: "Login", m ...

Mastering the Art of Trimming with Jquery

function DisplayDataForEdit(id) { $("#editContainer").slideDown("medium"); var parentId ='item'+ id; var colIndex = 0; var $row = $("#" + parentId).parent().parent(); $row.find('td').each(f ...

Leveraging the wheelDelta property in the onmousewheel event handler with Script#

When working with Script#, I often utilize mouse events using the ElementEvent argument. However, one thing seems to be missing - the WheelDelta property for handling the onmousewheel event. Can anyone provide guidance on how to access this property in t ...

Updating meta tags dynamically in Angular Universal with content changes

Hello, I'm encountering an issue with a dynamic blog page. I am trying to update meta tags using data fetched from the page. Here's the code snippet: getBlogPost() { this.http.get(...) .subscribe(result => { this.blogPost = re ...

Using HTTP POST in a Firefox Extension using JavaScript

As a beginner, I am attempting to execute a basic HTTP post in JS using a Firefox extension. I'm encountering an issue where the parameters are not being passed as expected: var params = "a=1&b=2&c=3" req.open('POST', 'http:// ...

Obtain the data from a different HTML element

When a user clicks on a button, I want to send the value of an input element in Angular2. What would be the most effective approach for achieving this? <input type="text" class="form-control" placeholder="Search for images..." /> <span class="i ...

Issue with random pages where global header.php is not showing a specific image

I'm currently revamping a website that was originally developed using codeigniter. The backend is quite chaotic with no clear identifiers for anything. I recently updated the banner in the header.php file, adjusting the style to suit the requirements. ...

Discover the best way to transfer hook values to CreateContext in React

In my project, I've implemented a component called SideBarBlurChange. Within this component, there is a requirement to pass a value named values inside the BlurChangeValue array, which is nested within the CreateContext. I have searched online for ex ...

Adding an HTML element to the DOM in an AngularJS directive without using jQuery

Looking to enhance my AngularJS directive by including an svg tag within the current element, currently using jQuery for this purpose. link: function (scope, iElement, iAttrs) { var svgTag = $('<svg width="600" height="100" class="svg">< ...

Issue with HTML Form Data Not Being Reflected in SQL Database

After creating a form to insert values into the database, there seems to be an issue where the database is not being updated despite no errors. Here is the HTML code for the page: <!DOCTYPE html> <html> <title>css</title> <body ...

Every time I attempt to visit my website on a mobile device, it seems to appear larger than normal, as

After developing a responsive website and adding media queries to make it mobile-friendly, I encountered an issue. Despite my efforts, the website's width is still larger than the mobile viewport, requiring users to zoom out to view it properly as sho ...

Properly aligning text with checkboxes using HTML/CSS and tags like <span> or <div>

My goal is to have the text displayed as a block in alignment with the checkbox, adjusting based on the sidebar's width. For reference: Current Layout Preferred Layout I have shared the code on CodePen (taking into account screen resolution and wi ...

Troubles with horizontal list incompatibility in Internet Explorer 6 and 7

I am facing an issue with displaying an unordered list in IE6+7. The problem arises when styling the list items with specific width and height properties, causing IE to stack the items vertically instead of horizontally. Below is my code snippet: For live ...

Utilizing a JavaScript variable to fetch a rails URL: A comprehensive guide

One interesting feature I have is an image link that has a unique appearance: <a href="#user-image-modal" data-toggle="modal" data-id="<%= image.id %>"><img class="user-photo" src="<%= image.picture.medium.url %>" alt="" /></a&g ...

Require assistance in accessing the second tab on a webpage using JavaScript tabs

Currently, I have a web page with two tabs that are initially hidden until the corresponding tab button is clicked. The click event is managed by jQuery. For example, let's assume that tab 1 is the default one when the page loads. Now, I am looking fo ...

Retrieve the data from the file input field

I am attempting to dynamically change the value of an input type text based on the input type file selection. Despite my efforts with the code below, I have been unable to find a solution to this issue. HTML: <input type="text" name="imagem" class="fi ...

React Side Panels that Collapse and Expand

Apologies if this task seems simple, as I am new to transitioning to React. My current application is primarily built with JavaScript, CSS, and HTML, and I am now looking to migrate it to React. There is a full-length horizontal side panel that is initiall ...

What is the reason behind React re-rendering child components despite passing props that have been memoized with useMemo?

While exploring this topic, I stumbled upon an answer that seems relevant: When does React re-render child component? However, my inquiry delves into a more intricate question. Why does React typically re-render child components when utilizing the useMemo ...