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

Error: Trying to access the length property of an undefined or null reference in JSON formatted data fetched through Ajax

I am attempting to populate a jQuery DataTables with JSON data retrieved from a Web API ajax call, but encountering the following error: An unhandled exception occurred at line 38, column 314 in 0x800a138f - JavaScript runtime error: Unable to get proper ...

Update ng-Bootstrap ToDate field by removing the date when selecting a fromDate

Is there a way to clear the date from the toDate input field while selecting a date from the fromDate input field in ng-bootstrap? <form class="row row-cols-sm-auto" *ngIf="showDateRangeImp"> <div class="col-12" ...

"Learn how to use socket.io in conjunction with the express generator to create individual sockets for each unique link

I'm working on a typical express-generator app where I have some code in my controllers folder: router.get('/:id([A-Za-z0-9_]{5,25})', function(req, res, next) { res.render('my-page.html', { title: 'Messag ...

Setting the 'redirect_uri' for Identity Server 4 in a React JS application and directing it to a specific view using a route

After following the instructions at , I attempted to login to Identity Server from my ReactJS application. Upon successful login, http://localhost:3000/callback.html was loaded with id_token and access_token in the URL. However, I noticed that this callbac ...

Keeping the state of a React component intact when navigating back from the router

Imagine you have two React components, A and B. As component A is displayed on the page, the user makes changes that affect some of the states within A. The user then clicks a button to navigate to component B using router.push('/b'). Subsequentl ...

Troubleshooting an Issue with Image Loading in Angular's UI-Bootstrap Carousel

var app = angular.module('app',['ui.bootstrap']) app.controller('getResources',['$scope',function($scope){ $scope.myInterval = 5000; $scope.noWrapSlides = false; $scope.active = 0; var slides = $scope ...

Delete the class once the image has finished loading

Is there a simple way to use JavaScript to implement Bootstrap 5 placeholders for lazy loading each image I load? Bootstrap 5 provides a placeholder class that displays a "loading card" while an image is loading. I want to utilize this class until each im ...

Utilize Jquery Loop to Showcase JSONP Array

I've gone through a multitude of similar questions, attempted to implement the solutions provided but still can't quite seem to make it work. I'm pretty sure it's just a simple mistake since I'm new to this. I have removed the URL ...

Employing the CSS not selector within JavaScript

I am facing an issue where my form darkens with the screen every time I click a button to show it, using JavaScript. If I were using CSS, I know I could use the :not selector, but I need guidance on how to achieve this in JS. Can anyone assist me? Below i ...

Tips for ensuring a custom list item remains at the top when the screen size decreases

My unordered list with custom bullets is displaying some unexpected behavior. When the screen size drops below 364px, the list items wrap to two lines. However, the custom bullet ends up on the second line instead of the first. How can I fix this for smal ...

Inverted CSS Shadow Box

I thought I had a good grasp on how CSS shadow-box works, but it seems I was mistaken. Could someone provide me with a visual explanation of why the <hr/> looks the way it does here? Take a look at style 13: https://codepen.io/ibrahimjabbari/pen/ozi ...

error : failed to establish a connection to the MongoDB database

Ensure that the first parameter passed to mongoose.connect() or mongoose.createConnection() is a string. MongooseError: The uri parameter for openUri() must be a string, but it was "undefined". Double check that the initial parameter for mongoose.connect() ...

Incorporating the Results of a Script into TinyMCE with the Help of

Recently, I developed a script for creating a basic table layout structure. To enhance its functionality, I decided to integrate AJAX with jQuery in order to input the output of the script into a TinyMCE textarea. The PHP script itself works seamlessly whe ...

Quick tip on closing an Angular-ui modal from a separate controller

I am currently using Angular-ui to display a modal with a form inside. Here is the code snippet: app.controller('NewCaseModalCtrl', ['$http', '$scope','$modal', function ($http, $scope, $modal, $log) { $scope.ite ...

Addressing see-through box styling in CSS

When working on a website using HTML and CSS, I encountered an issue with a transparent box. All elements within this box are also transparent, but I want them to be solid without any opacity. How can I resolve this? .parent { display: inline-block; ...

Include an Open OnClick event in the React/JSX script

We have a dynamic menu created using JavaScript (React/JSX) that opens on hover due to styling. My inquiries are: Instead of relying on CSS for the hover effect, where in the code can I implement an OnClick event to trigger the menu opening using Java ...

How can you securely transfer the ARRAY OBJECT from JavaScript to PHP using Ajax?

I am attempting to send a Javascript Array Object via Ajax to PHP and then use that object as a true Array on the PHP side. My current approach has not been successful in terms of utilizing the incoming object as an Array within PHP; I can only parse it as ...

How PHP Processes Fragment Identifiers within URLs

Looking for some advice from the community on a tricky situation I'm facing. Here's the issue at hand: I have developed a website with dynamic content pulled via AJAX and displayed using JS. To allow direct links to my content, I modify the frag ...

Can the geocoder API/search box be utilized to locate specific markers on a map?

Incorporating Mapbox into an Angular application with a high volume of markers on the map (potentially thousands) and hoping to implement a search box for users to easily locate specific markers based on unique names and coordinates. Is this functionalit ...

Spin a Material UI LinearProgress

I'm attempting to create a graph chart using Material UI with the LinearProgress component and adding some custom styling. My goal is to rotate it by 90deg. const BorderLinearProgressBottom = withStyles((theme) => ({ root: { height: 50, b ...