The Bootstrap carousel refuses to slide when new dynamic data is introduced

I imported data from a database, but the items are displaying separately and do not change or slide as they should. I attempted to use JavaScript to fix this issue, but unfortunately, it did not work. When I tried using the carousel-item class, it only made matters worse. So, now I am referring to this link because they utilize the item class. Below is an image showing what I am trying to achieve.

//html page

 {{Form::open(array('action'=>'HomeController@show_news', 'id'=>'myform'))}}
    <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <?php if (isset($news)) { $i=0; ?>
        <div class="carousel-inner">
            @foreach ($news as $row)
                <div class="item active" onclick="javascript:getimageid('{{$row->id}}')">
                    <img src="data:image/png;base64,{{base64_encode($row->img_png)}}">
                    <div class="carousel-caption">
                        <h4><a href="#">{{$row->title}}</a></h4>
                        <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
                            tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
                        </p>
                    </div>
                </div>
            @endforeach
        </div>
        <ul class="list-group">
            @foreach ($news as $rows)
                <?php $i++ ?>
                <li data-target="#myCarousel" data-slide-to="{{$i}}" class="list-group-item">
                    <h4>{{$rows->title}}</h4>
                </li>
            @endforeach
        </ul>
        <?php } ?>
        <input name="post_id[]" id="post_id[]" type="hidden">
        <script type="text/javascript">
            function getimageid(id) {
                $('[id^=post_id]').val(id);
                document.getElementById('myform').submit();
            }
            $(window).load(function() {
                $('#myCarousel').carousel({
                    interval: 1000
                })
            });
        </script>
        <!-- Controls -->
        <div class="carousel-controls">
            <a class="left carousel-control" href="#myCarousel" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left"></span>
            </a>
            <a class="right carousel-control" href="#myCarousel" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right"></span>
            </a>
        </div>
    </div>
    {{Form::close()}}

//css style

#myCarousel .carousel-caption {
    left:0;
    right:0;
    bottom:0;
    text-align:left;
    padding:10px;
    background:rgba(0,0,0,0.6);
    text-shadow:none;

}

#myCarousel .list-group {
    position:absolute;
    top:0;
    right:0;
    width: 33.5%;
}
#myCarousel .list-group-item {
    border-radius:0px;
    cursor:pointer;
}
#myCarousel .list-group .active {
    background-color:#eee;
}

@media (min-width: 992px) {
    #myCarousel {padding-right:33.3333%;}
    #myCarousel .carousel-controls {display:none;}
}
@media (max-width: 991px) {
    .carousel-caption p,
    #myCarousel .list-group {display:none;}
}

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

https://i.sstatic.net/2iPO3.jpg

//after changing item to carousel-item

Answer №1

To ensure the carousel functions correctly, only one element should be marked as active.

You can achieve this by implementing a code snippet like the following:

@foreach ($news as $index => $row)
    <div class="item <?php if ($index == 0) echo "active"; ?>" onclick="javascript:getimageid('{{$row->id}}')">

By doing so, the first item will be set as active while concealing the other images.


Prior Solution

It seems that you are lacking essential CSS classes for proper functionality. Consider adding the carousel and slide classes to your form element (a div may be more appropriate than a form, unless otherwise required). Additionally, ensure that items within the carousel have the class carousel-item rather than just item.

Modifying the initial line with

{{Form::open(array('action'=>'HomeController@show_news', 'id'=>'myform', 'class'=>'carousel slider'))}}
and updating the item tag with
<div class="carousel-item active" onclick="javascript:getimageid('{{$row->id}}')">
should result in carousel-like behavior.

Strive to mimic the documentation's appearance as closely as possible.

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

Tips for minimizing separation by utilizing timeline panels in Bootstrap

Is there a way to decrease the spacing between each panel? Right now, they are too far apart and when I fill up a lot of space, the navigation bar always shows up. I don't want that horizontal bar to be visible. I want to avoid that bar from appearin ...

Showing one error message at a time on AngularJS interface

Currently, I am developing an application using ionic-angular. I am facing a challenge with validation on the 'User Profile' page. The input fields on this page include 'First Name', 'Last Name', 'Cell Phone', ' ...

I am currently using React to implement a feature that displays random facts for 5-second intervals. Despite no errors being displayed, the facts are not appearing on the page as expected

Client side My react application includes a section that is supposed to display random facts at 5-second intervals. Although no errors are displayed, the facts do not appear on the page when I run the code. import React from "react"; import &quo ...

Retrieving registered components dynamically in Vue.js

Check out my scenario on jsBin In my setup, I have a selector <component :is="selectedComponent"></component>, along with a <select v-model="currentComponent">...</select> that allows me to choose which one is displayed. Currently ...

Customize selection options - AngularJS

I am working on an AngularJS form that includes a select option to create a restaurant entry in my database. <div id="name-group" class="form-group" ng-init="getClasses();"> <div class="select-wrapper"> <label for="location"> ...

Stand out with Bootstrap's wide card design

Does anyone know how I can correct the image display issue within this card using Bootstrap 4? I am attempting to adjust the picture so that it is perfectly scaled to fit within a fixed height card. Currently, the image appears stretched and I am explorin ...

The req.body variable is not defined in this context, which means only the default MongoDB id is

I've been attempting to utilize a post request for inserting data into MongoDB using Mongoose, but I keep encountering an issue where req.body is undefined. The document gets inserted into the database, but only the default Object ID is included. Thi ...

Error: Unable to access the 'center' property of an undefined value in the three.module.js file

I started delving into coding a few months back, with my focus on mastering three.js/react. Currently, I am engrossed in a tutorial located at [https://redstapler.co/three-js-realistic-rain-tutorial/], where I aim to create a lifelike rain background. The ...

Is there a way to eliminate the surplus 2 download icons from this Angular code when there are a total of 3 users?

Here is some HTML code snippet: <tr *ngFor="let user of users"> <td> <h6 class="font-medium">{{user.id}}</h6> </td> <td> <h ...

Is there a way to make the navigation menu collapse only on mobile devices and not on desktop?

I have been attempting to create a collapsible nav menu that only collapses on mobile devices, and not on desktops. When I include the code data-toggle="collapse" data-target="#navbarResponsive", it successfully collapses on mobile, but then the nav items ...

Attempting to import a npm module that is not defined

I recently released an npm package. It is working perfectly when accessed via the global variable window.Router in the browser, but I'm facing issues when trying to import it using ES modules in a Meteor application. It keeps returning undefined... ...

Events trigger React to render multiple times

I have implemented socket functionality on my website where users can send a word to the server, triggering an event (art-addpic) that broadcasts an image URL corresponding to that word to all users. However, only users with isArtist=true are allowed to re ...

Utilizing NodeJS, Express, and the Jade template engine to insert JSON data into

While trying to follow a tutorial on NodeJS, Express, Jade, and MongoDB, I ran into a frustrating issue. Despite reading through various forum threads and attempting the suggested solutions, I still can't seem to make the POST command work. Please di ...

TypeScript and the Safety of Curried Functions

What is the safest way to type curried functions in typescript? Especially when working with the following example interface Prop { <T, K extends keyof T>(name: K, object: T): T[K]; <K>(name: K): <T>(object: T) => /* ?? */; ...

Understanding how to translate the content of email confirmation and password reset pages in Parse Server

Is there a way to translate the Email Confirmation and Password Reset pages in my project using Parse server? I have searched everywhere for a solution but haven't found one yet. While I did come across information about email templates, I couldn&apos ...

The latest version is available, but remember to update @react-navigation/bottom-tabs, @react-navigation/stack, and @react-navigation/drawer to at least version 5.10.0

As a newcomer to react-native, I am currently attempting to execute a program using expo but encountering a yellow error message. The error states: 'It seems that you are utilizing an outdated version of the react-navigation library. Please ensure th ...

ng-admin fails to identify custom field view

I've been exploring ng-admin. After referring to the documentation, I decided to create a custom field. However, even though ng-admin recognizes the FooFieldType, it is not rendering the FoofieldView and instead using the original FieldView! Conf ...

Implementing JavaScript alerts with the Internet Explorer WebDriver and Selenium

Currently, I am facing a challenge with a Java based application where I need to click on a cancel button. I am using IE Driver, Eclipse IDE, and my application only supports IE. [I am scripting in Java] Here is the scenario: Login to the application An ...

Organizing JSON data based on years/dates (JSON with nodeJS)

Hello there, I have a JSON data structure that resembles the following: news { title: 'hello world', body: 'bla bla bla', publish_date: '2014-04-12' }, { title: 'hello world&ap ...

Pictures appear stretched on mobile browsers

The issue with picture display differences between my phone and computer When viewing pictures on my mobile browser, the height of the images appears stretched compared to when viewed on a computer. ...