Recalling the position of the uploaded image within a v-for loop

I am struggling to solve this issue.

Currently, I am utilizing Vue.js in an attempt to construct a dashboard where users can upload up to five images of visitors who are authorized to access a specific service provided by the user.

Below is the code snippet:

<div class="row" v-for="(n, i) in 5" :key="n">
  <div :id="'popover' + visitor.id + '-' + i" variant="primary">
    <div class="card visitor-image">
      <b-popover :target="'popover' + visitor.id + '-' + i" triggers="click focus">
        <template slot="title">Edit image</template>
        <button
          v-if="checkImage(i)"
          class="btn btn-danger image-btns"
          @click="deleteImage(visitor.id, visitor.Photos[i].id)"
        >Delete</button>
        <p
          v-if="uploadProgress < 100 && uploadProgress > 0"
          class="progress-text"
        >Upload progress: {{ uploadProgress }}%</p>
        <label
          v-if="!checkImage(i)"
          :for="'image-input' + visitor.id + '-' + i"
          class="btn btn-primary image-btns"
        >Upload</label>
        <input
          class="profile-image"
          :id="'image-input' + visitor.id + '-' + i"
          type="file"
          :ref="'fileInput' + visitor.id + '-' + i"
          style="display: none"
          accept="image/gif, image/jpeg, image/png"
          placeholder="Upload"
          @change="selectFile($event, visitor.id, 'fileInput' + visitor.id + '-' + i)"
        >
      </b-popover>
      <img
        class="card-img-top profile-image"
        v-if="checkImage(i)"
        :src="getImage(i)"
        :id="visitor.Photos[i].id"
      >
      <img class="card-img-top avatar-image" v-else src="../assets/avatar.png">
    </div>
  </div>
  </div>

In this scenario, the functionality of the functions is not crucial; the main concern lies with the "v-for" loop I am utilizing. The objective is to iterate through all visitors and their respective images while binding a unique key for each one.

I aim to understand how I can utilize that key so that when I click on one of the avatar images to upload a photo, said photo gets uploaded to the corresponding div. At present, regardless of which div I click on to upload an image, it always displays in the first div, even if I select the last div in the list.

Appreciate your help!

Answer №1

I just finished setting up the codepen for uploading files within a v-for loop. I implemented splice to add an image at a specific index. Take a look at the codepen provided below for more details.

Codepen Link - https://codepen.io/unknownuser/pen/XGYoyr

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

A centered Bootstrap navigation bar on a WordPress site

My Bootstrap navbar is floating in the center on WordPress and I'm not sure why. Can anyone help me with this issue? Could it be related to WordPress or my stylesheet problems? (Will update my code here if you need) STYLES.CSS .navbar-default { ...

I am having trouble figuring out the issue with the state and how to properly implement it in Typescript

I am having difficulties sending emails using Nodemailer, TypeScript, and NextJS. The contact.tsx file within the state component is causing errors when using setform. As a beginner in TypeScript, I have been unable to find a solution to this issue. Any he ...

JQuery - Triggering mouseenter/hover event only on top-level menu items, excluding the nested submenu items (list items that are not within nested lists)

I have a complex navigation menu structure with nested lists, and I'm struggling to trigger an event only on the top-level items when hovered. Despite trying different jQuery selectors and methods, such as using the NOT selector or targeting direct ch ...

Locate the closest Vue parent component containing the template reference (Vue 3)

After a Vue template ref is initialized, my goal is to identify the closest parent Vue component. To make this functionality versatile and applicable to any template ref, I have encapsulated it within a composition function (although that's merely an ...

What is the best way to combine PHP and HTML within a JavaScript script?

I'm facing a challenge with integrating dynamically added elements and a populated drop down on my form. Is there a way to combine the two seamlessly? Below is the code snippet I'm using for dynamically adding and removing elements: $(docu ...

The Role of Filling in a Process

I am looking to create a rectangle that fills up gradually every day, increasing by 1% each time. This is the basic concept. My main struggle is figuring out how to fill it up. I need the rectangle to increase by 1% of its width each day. So essentially, ...

Following the completion of the $axios.post request, a redirect to a different page is encountering issues

Is it possible to prevent moving the page after a post request? I'm encountering an issue with redirection after using $axios.post. Any ideas on what might be causing this problem? Could there be an error in my code? <template> <v-form re ...

Should we define all public methods or prototype each method separately?

Typically, when I create a library, my approach is as follows: var myLibrary = (function() { return { publicProperty: 'test', publicMethod: function() { console.log('public function'); }, ...

Tips for eliminating the default hover and active states from a textfield in Material UI

Currently, I am in the process of creating a login form and have incorporated Material UI textfields. However, I am facing an issue where the textfield's default hover and active states cannot be removed. Below is my password textfield: (https://i.st ...

jQuery error: an unexpected token was encountered

I am encountering an issue with an "unexpected token =" error on the line toggleNav = function(evt){ in the code snippet below. Despite going through various similar posts, I am unable to pinpoint why this error is occurring. Any assistance in guiding me ...

Ways to prevent the need for multiple if/else statements and repetitious function instances

Check out this code snippet in Javascript: https://pastebin.com/zgJdYhzN. The purpose of the code is to fade in text when scrolling reaches a specific point. While it does work, I want to optimize it for multiple pages without creating separate instances ...

How to send URL parameters to a different page with the help of express and Node.js

Hey there, I'm currently working on a chat app which you can check out here. I'm in the process of enabling users to join specific rooms by typing in a URL like , assuming they are logged in. I manage user login and signup with passwords. Here&ap ...

Implementing Custom Font Awesome Icons in Your Angular Project

I recently upgraded to a fontawesome subscription with a paid plan and have successfully created some custom icons. Now, I'm looking to integrate these icons into my angular app. Here are the dependencies listed in my package.json file: "@fortawe ...

Implement a callback function to dynamically generate variables and display them within an HTML element

Apologies in advance for any errors I may make as I am a beginner when it comes to javascript, jquery, and json. I have a script that retrieves data from a json file and displays it on a webpage using javascript, jquery, ajax (I believe), and json. When ...

Preventing mouse clicks on checkboxes and triggering events using JavaScript - a complete guide

We have a Table grid with multiple columns, one of which is a Select Box (CheckBox). The expected behavior is that when a row is clicked, the respective CheckBox should get checked, and clicking on the CheckBox itself should update it. I tried implementin ...

Using ASP.NET MVC to map FormData with an array to a model class

I am currently facing an issue with mapping a list of objects in a FormData to my ASP.NET MVC Model class at the controller. I have successfully sent the FormData over to the server-side, but I am unable to bind any value. Can someone provide guidance on h ...

What is the best way to transfer a request parameter from a URL to a function that generates an object with matching name in JavaScript?

I need to figure out how to pass a request parameter from an express router request to a function that should return an object with the same name. The issue I'm facing is that the function is returning the parameter name instead of the object. Upon c ...

Is there a way to arrange an array based on the initial value?

In my array, I have a mixture of strings and arrays. Each string corresponds to the array next to it. For example, the string 789 is associated with the array ['111A', '222B', '333C']. My goal is to sort based on the strings w ...

Utilizing ES6 Map type in TypeScript for Angular 2 Response Data Transfer Object Definition

Is it possible to utilize the es6 Map type in an HTTP Response DTO? Let's consider an Angular 2 request: public loadFoos(): Observable<FoosWrapper> { return this.http.get("/api/foo") .map(res => res.json()); } Now, take a loo ...

Developing an interface that utilizes the values of an enum as keys

Imagine having an enum called status export enum status { PENDING = 'pending', SUCCESS = 'success', FAIL = 'fail' } This enum is used in multiple places and should not be easily replaced. However, other developers migh ...