Only retrieve one result from v-for loop depending on the key value

My v-for loop is set up to display a single result based on the chosen :key value from the counter data property. Everything is working well, but I am encountering problems when I try to add <transitions> for a smoother update of the value. The issue is that both the old and new items appear briefly and the transition causes them to jump around the page.

It seems like the tag elements are all present in the DOM due to the v-for loop, and they are just transitioning between each other.

Is there a more efficient way to achieve this behavior so that only the {{tag}} values get updated based on the key?

<div v-for="tag in tags" :key="tag.id">
     <transition name="fade">
      <div v-if="tag.id == counter">
        <div class="tag-col--prod-img mb-4">
          <img class="img-fluid" :src="tag.thumb" />
        </div>
        <h5 class="mb-5">{{tag.heading}}</h5>
        <div class="mb-3">
          <h1>{{ tag.title }}</h1>
        </div>
        <h2 class="mb-3">{{ tag.price }}</h2>
        <p class="mb-4">
          {{tag.detail}}
        </p>
        <a :href="tag.link" target="_blank">
          <button class="btn btn-primary">View product</button>
        </a>
       </div>
      </transition>
</div>

Answer №1

Learn how to utilize Vue's computed properties with this easy technique:

<transition name="fade">
  <h5 class="mb-5">{{activeTag.heading}}</h5>
  <!-- Additional content here -->
</transition>

Simply add this code snippet to your component:

computed: {
  activeTag() {
    return this.tags.find(({ id }) => id === this.counter);
  }
}

When either tags or counter is changed, the activeTag will be reevaluated, automatically updating the relevant DOM elements.

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

Issue with Angular 2 - Struggling with Routing

My objective is to create a menu with four links, each leading to a different HTML page: <p> <a routerLink="/function">Business Function</a> <a routerLink="/process">Business Process</a> <a routerLink="/appsystem"> ...

Utilizing AngularJS to Retrieve URL Parameters Within a Controller

I am attempting to retrieve URL parameters in my controller: Although I came across this example, I encountered an error that appears to be connected to the loading of necessary modules. app.controller('WidgetCtrl', ['$scope', '$ ...

What is the recommended lifecycle hook in Vue.js2 to execute a function when the page is loaded?

I have a dynamic table that can be filled with various numbers of rows, and I want to add an overlay before the data is loaded using my applyOverlay() function. Below is the structure of my HTML: <table id="table" class="datatable" s ...

Reduce the amount of ajax calls

Currently, I have implemented checkboxes that serve as search filters on a website. Every time a user checks a filter box, an ajax request is triggered to fetch data from the server and display it. The issue arises when users select multiple checkboxes in ...

Preserve the existing value and then check it against the updated value of a variable within JavaScript

I utilized an API that supplies me with information in JSON format, retrieved the price of a specific currency, and presented it on a screen using JavaScript. I encapsulated this process within a function that dynamically updates the information at set int ...

A problem arises when utilizing jQuery's $.isArray functionality

Successfully executing an AJAX post, the function test is utilized to process the passed data. $("#formID").submit(function (event) { $('#postError').html('').hide(); $('#postInfo').html('loading results').s ...

Is there a bug in Safari 8.0 related to jQuery and backslashes?

I am using Mac OS 10.10 Yosemite and Safari 8.0. Attempting to read an XML (RSS) file: <content:encoded>bla bla bla</content:encoded> The Javascript Ajax method I am using is: description:$(valeur).find('content\\:encoded&apo ...

Using JavaScript functions within PHP is not supported

Currently, I am utilizing Laravel for my backend operations. Within my setup, there exists a JavaScript function called StartJob() that facilitates Google crawling. In the scenario where I input a keyword, which is then cross-referenced with the database, ...

Unable to `.catch()` an error while utilizing Jquery.ajax().then()

My current project involves making calls to various APIs using JQuery and caching the response from each API. This cached data is then used multiple times on the page to create different dashboard widgets. The issue I'm facing is that if an API retur ...

Iterate through a generic array to populate a table using ng-repeat

In the scenario I'm currently facing, I am working on creating a common reusable table structure using a directive. The goal is to display different JSON data in both the table header and body. Below is the controller code: angular.module('plun ...

Discovering the selected option's value in a dropdown and utilizing it to search through an array for data to fill another input

Is there a way to utilize the value of a select input option, created by a forEach loop, to extract the value of another property in the same array object and assign it as the value of a different input field? Apologies if this seems verbose or has been a ...

How can you efficiently access the 'app' object within a distinct route file?

When using Express 4, the default behavior is to load routes from a separate file like so: app.use('/', routes); This would load routes/index.js. I am working with a third-party library that directly interacts with the app object itself. What& ...

Can someone explain the distinction between 'return item' and 'return true' when it comes to JavaScript array methods?

Forgive me for any errors in my query, as I am not very experienced in asking questions. I have encountered the following two scenarios :- const comment = comments.find(function (comment) { if (comment.id === 823423) { return t ...

Switch between Coordinated Universal Time and a designated time zone using the New Internationalization API

I am experimenting with the new Internationalization API using Chrome Version 31.0.1623.0 canary. My goal is to: Convert Date Time between UTC and a specific time zone (America/New_York as an example). Determine if the conversion takes into account Dayl ...

Exploring methods of simulating properties using jest.fn()

Here is the code snippet I'm currently working with: this.$vs.loading() .... this.$vs.loading.close() To successfully pass a mock to shallowMount, I have set it up as follows: const vs = { loading: jest.fn() } mocks: { $vs: vs } However, the ...

What is the best method for deleting a portion of a string following the final instance of a particular character?

I have a single string that looks like this: "Opportunity >> Source = Email >> Status = New >> Branch = Mumbai" My goal is to truncate the string from the last occurrence of >>. Essentially, I want the resulting string to be: "Op ...

Traversing a JavaScript array with multiple dimensions containing markers created with Google Maps APIs

I have a single array where I store all of the Google Maps marker objects. Currently, I am working on creating a function to remove all markers from the map that are in the array, but I'm facing issues with the loop. First, I add each marker to the a ...

Open Modal in Vue-Router Using Code

My current setup involves a login page that redirects users to it if they are not authenticated (verified via the meta tag auth). On the other hand, if a user is already logged in and tries to visit the login page, they are redirected to the home page (det ...

updating the model when the view input element is changed, without the model affecting the view input element

Does anyone know of a way to achieve one-way binding that updates the model when the value of a number input is changed, but prevents the input element from being updated when the model changes? I prefer not to use v-model or :value, as they are two-way ...

Retrieve active route information from another component

We are utilizing a component (ka-cockpit-panel) that is not linked to any route and manually inserted into another component like so: .. ... <section class="ka-cockpit-panel cockpit-1 pull-left"> <ka-cockpit-panel></ka- ...