Updating values dynamically using AJAX within a v-for loop

I'm struggling to figure out how to change the value of an attribute in a v-for loop.

For instance, I would like the index to be used as the name of the related product:

HTML

<div v-for="(index, publication) in publications">
  {{ index | name publication.productId }}
</div>

JS

 filters: {
      name: function(val, productId) {
        this.$http.get('/api/product/'+productId)
          .then(function(rst) {
          return rst.data.name;
        }).catch(function(err) {
          console.error(err);
        });
      }
    },

The request successfully returns the name, but it doesn't seem to appear in the HTML template. I suspect this is because the filter is synchronous.

If anyone has a solution, I'd greatly appreciate hearing about it!

Answer №1

Using a computed property might not be the best approach for your situation. Consider utilizing a real data item and updating it within a watch of publications. Here's an example:

data: {
  ...
  productData: []
},
watch: {
  publications: function (newValue) {
    this.productData = [];

    this.publications.forEach((pub, i) => {
      this.$http.get('/api/product/'+pub.productId)
      .then((rst) => {
        this.productData[i] = rst.data.name;
      }
    });
  }
}

You can then use a v-for directive like this:

<div v-for="name in productData">
  {{ name }}
</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

Upon clicking, Vue triggers form validation in Laravel

I have encountered an issue that I am unable to solve by myself: Within my Laravel view, I have included a form in the following manner {!! Form::open(array('route' => ['reports.store', $project->id], 'data-parsley-validate& ...

Managing the response from a dataless AJAX GET request

I find myself stuck on a seemingly simple issue. I am currently working on refining a basic AJAX GET request function that deals with response handling. There is no data being sent out with the request; instead, the data is managed through the GET URL. Whi ...

Pass information from the child component to the parent component using Vue.js

I am currently working on a modal component named modal_form.vue, which I am using inside another component called index.vue. My goal is to pass a data variable from modal_form.vue to index.vue. Can anyone provide me with an example of how to achieve this ...

What is the best way to prevent the body from scrolling when scrolling on a fixed div without making the body's scroll bar disappear?

Is there a way to prevent the body from scrolling while I scroll on a fixed div? I attempted using overflow:hidden for the body, which stops scrolling but causes the page to shake when the scroll bar disappears. Is there a solution that allows me to keep ...

What are the best practices for implementing Laravel and Vue together?

Hello, I'm currently new to working with Laravel and Vue. I have successfully registered a component, but it's not displaying the content in my blade template. I would greatly appreciate any help or guidance on properly implementing this in Larav ...

What steps can I take to prevent receiving a 400 (Bad Request) error when using ajax PUT

Having an issue with sending data using JavaScript and AJAX. The objective is to send the data via PUT method to the server. var payload = 'id=' + id + '&brand=' + brand + '&model=' + model + '&country=' ...

"Experience the power of utilizing TypeScript with the seamless compatibility provided by the

I'm attempting to utilize jsymal.safeDump(somedata). So far, I've executed npm install --save-dev @types/js-yaml I've also configured my tsconfig file as: { "compilerOptions": { "types": [ "cypress" ...

Encountered an error: data.map is not functioning as expected in the React component

Hi there, I've encountered a small issue with my modal component. The error message I'm getting is: Uncaught TypeError: meatState.map is not a function. Any suggestions on what may be causing this problem? Your assistance would be greatly appreci ...

Animating overlapping div elements using jQuery

I am currently using jQuery's animate() function to adjust the size of a "row", but I am facing an issue with a badge that sometimes appears on the row. The problem is that the badge gets hidden during the animation, which doesn't look good. I ha ...

Creating an iterable type in TypeScript with key-value pairs - a beginner's guide

I am trying to define a type in TypeScript that represents an object with dynamically generated keys. How can I achieve this? { dog: true, cat: true, x: true } Currently, I am using the 'any' type but I would like to define a proper t ...

Uploading Files within Angular FormArray

I am facing an issue with my formArray which contains file upload inputs in each element. Whenever I upload an image in one input, it changes the values of other file inputs in different rows. https://i.stack.imgur.com/3haZW.png Current Behavior: Uploadi ...

Tips for choosing between options in JavaScript and AngularJS

How can I choose the appropriate <select> tag option using JavaScript or AngularJS in the backend? Hint: I receive data from an API service and populate a form for editing. Assuming the gender is currently set as Male in the database, how can I disp ...

Prevent the mouseup event in jQuery when the mouse has previously been moved

I have a div with a row of span buttons displayed horizontally. Since there are too many buttons to fit on the screen, I want to enable the user to click and drag the entire row of buttons. However, my challenge is to ensure that the button's mouseup ...

"Is there a way to ensure that jPlayer plays the same song even after a page refresh

I have been working on integrating jPlayer with Ajax to ensure that when the page of my website is refreshed, jPlayer will continue playing its current song seamlessly. I have successfully stored track information and the current track during refresh, ho ...

Uniting the graphical user interface with the server side operations

Greetings fellow developers! I am diving headfirst into the world of Stack Overflow and coding, so please bear with me as I navigate this new territory. Currently, I have crafted a sleek front end for a web application using a trifecta of HTML, CSS, and J ...

Modify the Primevue Class p-accordion-header only under certain conditions

I'm currently facing a challenge with customizing the styling of the primevue component class p-accordion-header based on the status of the rendered component determined by the variantNumber. Here's a snippet of my code: <Accordion :multiple= ...

Tips for accessing the value from an input field in an AngularJS application

Looking at my DOM structure below, <input class="k-textbox ng-pristine ng-untouched ng-valid ng-valid-required" type="text" placeholder="" ng-blur="controller.isDirty=true" ng-change="controller.isDirty=true" ng-model="controller.model.value" ng-requir ...

Pull data from one array of objects using the id from another array to display in a list using AngularJS ng-repeat

After retrieving a list of options, I make an API call to validate each option. The goal is to display whether each option is valid or not. My starting array looks like: $scope.preValidationArray = [ { id: 1, description: 'Item 1' }, { id: 2 ...

What method is best for deleting an item from the database in HTML structure - POST, GET, or XHR action?

I have a webpage that displays content in a table format and allows users to delete a specific row by clicking on it. The structure of my rows is as follows: foreach ($rewards as $reward) { echo '<tr id="' . $reward[&apos ...

Struggling to implement my reusable React component within a React Bootstrap modal

I have developed a reusable textarea React component: import React from 'react'; import '../form-input/form-input.styles.scss'; const TextAreaComponent = ({ handleChange, label, ...otherProps }) => ( <div className="group"> ...