Using a loop in a Vue.js slick slider: easy step-by-step guide

While utilizing vue-slick link https://www.npmjs.com/package/vue-slick within a bootstrap modal, I encountered an issue when using a v-for loop to iterate through the items. The problem can be seen in this example: https://i.sstatic.net/PhJCE.jpg.

Below is an excerpt of my code:

import Slick from 'vue-slick';
export default {
    props: ['vehicleRequest'],
    components: {'slick': Slick},
    data() {
        return {
            vehicle: {
                model: '',
                licensePlate: '',
                type: '',
                currentMileage: '',
                Availability: '',
                imageUrl: ''
            },
            vehicles: [],
            slickOptions: {
                infinite: true,
                slidesToShow: 5,
                slidesToScroll: 3,
            },
        }
    },
    mounted() {
        console.log("vehicleRequestId: " + JSON.stringify(this.vehicleRequest));
    },
    methods: {
        updated() {
            this.reInit();
        },
        reInit() {
            this.$refs.slickone.reSlick();
        },
        reserveVehicle() {},
        allocateVehicle() {},
        getVehicleRequest(id) {},
        approveOnline() {
            console.log("approve online!");
        },
        approveOffline() {
            console.log("approve offline!");
        },
        declineRequest() {
            $('#declineRequest-Modal').modal('hide');
            console.log("vehiclerequest: " + this.vehicleRequest);
            console.log("declined");
        },
        viewVehicle(vehicles) {
            let self = this;
            self.vehicles = vehicles
            $('#viewVehicle').modal('show');
        }
    }
}
<div 
    class="modal fade" 
    id="viewVehicle" 
    tabindex="-1" 
    role="dialog" 
    aria-labelledby="exampleModalCenterTitle" 
    aria-hidden="true"
>
    <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalCenterTitle">{{vehicle.licensePlate}}</h5>
                <button 
                    type="button" 
                    class="close" 
                    data-dismiss="modal" 
                    aria-label="Close"
                >
                    <i 
                        class="fas fa-times-circle" 
                        aria-hidden="true" 
                        style="font-size:20px;color: #6e6f6c;"
                    ></i>
                </button>
            </div>
            <div class="modal-body">
                <slick ref="slickone" :options="slickOptions">
                    <div v-for='vehicle in vehicles' :key="vehicle.id">
                        <img :src="vehicle.vehicle_image_url">
                    </div>
                </slick>
            </div>
            <div class="col-md-10 mx-auto mb-4">
                <div class="row">
                    <div class="col-md-6">
                        <div class="w-100">
                            <button 
                                type="button" 
                                style="margin-left: 25%; padding-left: 20px; padding-right: 20px;" 
                                id="alternativeVehicle" 
                                @click="allocateVehicle()" 
                                class="unicef-btn unicef-reset small-button"
                            >ALLOCATE ALTERNATIVE VEHICLE</button>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="w-100">
                            <button 
                                type="button" 
                                style="margin-left: -7%;" 
                                @click="reserveVehicle()" 
                                id="reserve" 
                                class="unicef-btn unicef-primary small-button"
                            >RESERVE</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Do you have any insights on what might be causing this issue?

Answer №1

Apologies everyone for my delay in sharing the solution earlier. Despite being busy, I managed to find a way to achieve what I was looking for. Although the CSS styling still needs some refining, I created a property called 'currentIndex' in the data and a computed property named 'currentVehicle'. By looping through 'currentIndex', I was able to retrieve the currentVehicle.

computed:{
    currentVehicle: function(){
        return this.vehicles[Math.abs(this.currentIndex) % this.vehicles.length];
    },
},

Therefore, the 'currentVehicle()' function will give me the index of the vehicle currently in view.

Check out the working solution here:

https://jsfiddle.net/nelsonk/ka01zvtm/

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

"Experience the power of MVC single page CRUD operations paired with dynamic grid functionality

Currently attempting to create a single page application CRUD functionality using UI Grid, however encountering an error during post request. ...

Are there any publicly accessible Content Delivery Networks that offer hosting for JSON2?

Everyone knows that popular tech giants like Google and Microsoft provide hosting for various javascript libraries on their CDNs (content distribution networks). However, one library missing from their collection is JSON2.js. Although I could upload JSON2 ...

The intervals in hooks seem to be malfunctioning and not updating the date and time as expected

I am trying to continuously update the date and time every minute using React hooks. However, I am facing difficulty in updating the time properly. const Link = (props) => { let date = new Date(); const [dateTime, setDateTime] = useState({ cu ...

Creating an object and setting its prototype afterwards

While exploring examples and questions online about prototypal inheritance, I noticed that most of them involve assigning prototypes to constructor functions. One common pattern is shown in the code snippet below: Object.beget = function (o) { var F = ...

A guide on accessing $bvToast in Vue class components

I am fairly new to vue.js but I have managed to figure out some things. I initially started with regular js, but then transitioned to typescript with class style vue components. For styling the components, I rely on bootstrap-vue. In my main.ts file, I im ...

What steps can I take to enhance my script and prevent the password field from being displayed alongside the username

I am currently working on a script that dynamically displays your username in the Login button as you type it. However, I have encountered an issue where the password is also being displayed, which is unacceptable. Additionally, I would like to enhance my ...

How can you make each <li> in an HTML list have a unique color?

Looking for a way to assign different colors to each <li> element in HTML? <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <ul> Here's how you want them displayed: Item 1 should be red Ite ...

Tips for integrating new channels and categories using a Discord bot

Hey there! I'm trying to add channels and categories, but I can't seem to get the function ".createChannel" working. The console keeps telling me that the function doesn't exist. I've been referencing the documentation at https://discor ...

What is the best way to store article IDs using Javascript or HTML?

On my web page, I have a collection of links that users can interact with by clicking and leaving comments. These links are loaded through JSON, each with its unique identifier. But here's my dilemma - how can I determine which link has been clicked? ...

Unable to render backend-sourced images in ReactJS

I'm currently working on a project that involves displaying images fetched from the backend. I've been successful in displaying all the data except for the images. Below is the response I received when I logged the response: [ { "_id&qu ...

Arranging HTML elements using JavaScript or jQuery

Something seems off. The sorting doesn't work as expected. Check out the JavaScript code below: function sortDescending(a, b) { var date1 = $(a).data('date'); var date2 = $(b).data('date'); return date1 > date2; } ...

How to ensure that the iframe is completely loaded before proceeding with Selenium JavaScript

I have a webpage that displays an iframe, within the iframe there is a spinning spinner (overlying the fields). My approach involves using selenium to navigate to the iframe, return this.driver.wait(function() { return self.webdriver.until.ableToSw ...

Populate a table with data from a different table using JavaScript

On my webpage, I have a grid of selectable divs that are defined by rows and columns. When I select certain divs, it creates what I'll call "table Copy", a three-dimensional table. If I select different elements, another three-dimensional table calle ...

Is it possible to create a website with a single session for all users using JavaScript

Greetings everyone, I am excited to be posting for the first time on stackoverflow. After dedicating a week to learning javascript and achieving some things I am proud of, I have hit a roadblock. As a self-learner, I kindly ask for your understanding. Cur ...

What is the best way to connect a data property to dynamic elements?

Once the page loads, an SVG file's content will be injected into the page. Is there a method to connect a data property to an element within the SVG in order to achieve something like this: <g :data-type="type">...</g> or perhaps this: ...

Having trouble accessing an element using jQuery(idOfElement) with a variable as the name parameter

Possible Duplicate: Issue with JavaScript accessing JSF component using ID Whenever I attempt to retrieve an element by passing a variable in jQuery(idOfElement), it doesn't work. But if I use something like jQuery('#e'), it works perfe ...

How can you update the background image of a particular div using the 'onclick' feature in React.JS?

Thank you for helping me out here. I'm currently working with ReactJS and facing a challenge where I need to change the background of a div from a color to a specific image URL upon clicking a button in a modal. Despite my efforts, I keep encountering ...

Ways to change object to string in javascript

My object has the following structure: Object {Dry Aged Ribeye(medium): "1" , Dry Aged Ribeye(rare): "1" , Dry Aged Ribeye(well): "1" , favorite_tables: "{"dc76e9f0c0006e8f919e0c515c66dbba3982f785":[]}" } I am trying to insert this into My ...

Route is searching for static files in the incorrect directory

There seems to be a specific route, /admins/:id, that is looking for static files in /public/admins/ instead of /public/. This issue is causing 404 errors and preventing the js and css files from loading on this page. All other routes are correctly fetchin ...

Iterating through an array with a .forEach loop and referencing the variable name

Currently, I am working on a project using JS/React and dealing with nested arrays in an array. The structure of my data looks like this: const ezra = ["Patriots", "Bears", "Vikings", "Titans", "Jets", "Bengals"] const adam = ["Chiefs", "Cowboys", "Packer ...