I'm looking to execute a Vue.js method within a template tag and display the data returned by the method. How

Currently, I am in the process of developing a web application using Laravel and Vue.js. As someone who is new to these frameworks, I am facing a challenge where I need to call a method inside a Vue component and display its return data within a v-for loop.

The IsAvailableRefund() function operates as follows:

At the create stage of the component, data is loaded and displayed in a table. When this data is loaded, the IsAvailableRefund() function is called with the invoice data passed to it. The return value is then shown in the table, enabling me to call it in the create function.

Below is an excerpt of my component code:

<tbody>
  <tr div v-for="invoices in invoice">
    <th scope="row">{{invoices.p_id}}</th>
    <td>{{invoices.p_amount}}</td>
    <td>{{invoices.p_qty}}</td>      
    <td>{{invoices.p_amount}}</td>
    <td>
      <!-- Here, I need to call the method and print its data: -->
      {{IsAvailableRefund(invoices.p_id,'002') }}         
    </td> 
  </tr>             
</tbody>

This is my script:

export default{        

    data(){
        return {
            invoice:{},         
        }
    },

    methods{

      IsAvailableRefund(p_id, invoice_id){    
        axios.get('/check-refund-data/'+p_id+'/'+incoice_id)
          .then(function (response) {
             console.log(response.data);
             this.refund = response.data;      
           })
       }
     },

    created(){
        axios.get('/refund-data/002').then(response => {
          this.invoice = response.data.invoice
          console.log(response.data.invoice);     
        })

        .catch(error => {
          console.log(error);
        })
    }
}

If anyone has any suggestions on how to solve this issue, please feel free to share. Thank you!

Answer №1

When it comes to retrieving data within your Vue component, it's essential to do so at the beginning of the Vue instance lifecycle (during onCreate, created, mounted, etc.). During this process, you have to alter an attribute (key in 'data') of the instance that has already been established since the initiation of the instance.

              <tbody>
                <tr div v-for="invoices in invoice">
                  <th scope="row">{{invoices.p_id}}</th>
                  <td>{{invoices.p_amount}}</td>
                  <td>{{invoices.p_qty}}</td>      
                  <td>{{invoices.p_amount}}</td>
                  <td>
                    <!-- here I need to call a method and print its data: -->
                    {{refund}}         
                  </td> 
                </tr>             
              </tbody>

script

    export default {        

        data() {
            return {
                invoice: {},
                refund: "" // data not fetched yet
            }
        },

        methods: {

          IsAvailableRefund(p_id, incoice_id) {
            axios.get('/check-refund-data/' + p_id + '/' + incoice_id)
           .then(response => {
            console.log(response.data);
            this.refund = response.data;      
           })
          }
        },

        created() {

            axios.get('/refund-data/002').then(response => {
            this.invoice = response.data.invoice
            console.log(response.data.invoice);
            IsAvailableRefund(invoices.p_id, '002')
            })

            .catch(error => {
            console.log(error);
            })
        }
    }

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

``Protect your PDF Document by Embedding it with the Option to Disable Printing, Saving, and

Currently, I am facing a challenge to enable users to view a PDF on a webpage without allowing them to download or print the document. Despite attempting different solutions like Iframe, embed, PDF security settings, and Adobe JavaScript, I have not been s ...

Exploring methods to trace the factory's property that is receiving updates from another factory within AngularJS

Hey there, I'm new to Angularjs and I have a bunch of factories in my application. The situation is, let's say we have obj1 in factoryA. Whenever I console.log(obj1), it displays numerous properties associated with it. This object is being update ...

Can you customize the buttons in the operation panel of an antd image preview window?

https://i.sstatic.net/hZLOn.png I am looking to add a button that allows users to download or share the image being previewed. Is there a way to achieve this functionality? I have researched and read through the documentation but have not found a solutio ...

Show various forms that gather information

Upon selecting an option from the drop-down menu, a certain number of forms will be displayed with captured data. For example: Imagine owning a form with fields such as No. of Applicants, Applicant Name, Telephone, E-mail, etc... If the user selects 2 fo ...

Using the async.waterfall function in an Express application

Query: I'm encountering an issue with my express.js code where, upon running in Node.js, an empty array (ganttresult) is initially displayed. Only after refreshing the browser do I obtain the desired result. To address this problem, I have attempted ...

Nav Bar Toggle Button - Refuses to display the dropdown menus

My Django homepage features a Bootstrap navbar that, when resized, displays a toggle button. However, I am experiencing difficulty with dropdown functionality for the navbar items once the toggle button is activated. Can anyone provide guidance on how to a ...

Can you recommend a straightforward method in Vue.js for validating the format of user input?

Here is how I have implemented an email sending feature in Vue.js: <template> <button @click="sendMail(); $emit('close')">Send</button> </template> <script> methods: { sendMail () { axios ...

A tutorial on how to switch classes between two tabs with a click event in Vue.js

I am having an issue with my spans. I want to implement a feature where clicking on one tab changes its color to red while the other remains gray. I have tried using class binding in my logic but it's not working. How can I solve this problem and make ...

In a multi-line chart, learn how to efficiently toggle the visibility of data points and lines

My goal is to toggle data points in my line chart by clicking on the legend. You can view my code here. Currently, when I click on the legend, the lines and corresponding axis are toggled on and off successfully, but the data points do not change. Below is ...

implementing a smooth transition effect for image changes upon hover

I've been working on a React project where I created a card that changes its image when hovered over. I wanted to add a smoother transition effect to the image change using transition: opacity 0.25s ease-in-out;, but it doesn't seem to be working ...

jQuery-powered Ajax file upload progress bar

Although I don't rely on jQuery for everything, I do find it useful for tasks such as AJAX. But currently, I'm facing some challenges with it. Here is a piece of code that should update my upload progress bar while the upload process is ongoing. ...

Tips for concealing items on a website within a WebView

Is it possible to hide the header element with the search bar and logo when loading this page on a Webview? I'm not familiar with JavaScript, so is there a way to achieve this? The section I am looking to conceal ...

Tips on how to properly display the date in v-Calendar

I'm struggling to format the date output from the v-calendar. Currently, it appears like: Fri Jul 31 2020 00:00:00 GMT+0200 (utc summertime) However, I would like it to display as 2020-07-28. I have searched through the documentation but couldn' ...

Transforming a flow type React component into JSX - React protocol

I have been searching for a tooltip component that is accessible in React and stumbled upon React Tooltip. It is originally written in flow, but I am using jsx in my project. I am trying to convert the syntax to jsx, but I'm facing difficulties with t ...

Sending a JWT token to a middleware with a GET request in Express Node.js - the proper way

Struggling with Js and web development, I've scoured the web for a solution to no avail... After completing a project for a small lab, my current challenge is creating a login page and generating a web token using JWT... I successfully created a use ...

Angular Elements - Additional Attribute Bindings

My goal is to create a series of versatile components (angular 1.5) with various optional bindings that can be utilized in multiple applications. I am concerned about the potential creation of unnecessary watchers for an application that does not utilize ...

How can you send query parameters to ajaxResponse in Tabulator 5.0 while using setData?

I'm currently facing an issue with utilizing both ajaxResponse and setData for a Tabulator 5.0 table. It seems that my URL parameters are disappearing somewhere in the process. When I log the value of params to the console in the code below, it appear ...

Sending an HTTP post request with form data and various field controls will not be directed to a Java backend

Recently, I've started working with AngularJs and I'm facing an issue with uploading image files along with their labels through a Jax-RS post rest API. My problem is that the control in my AngularJS controller is not entering the Java post API. ...

Ways to send data to nested elements when implementing secure routes?

I am currently working on a project to develop a 'Train Ticket Reservation System' using ReactJS. In order to access the services, users must login, so I have implemented protected routes to render certain components. Instead of relying on the de ...

Having trouble closing my toggle and experiencing issues with the transition not functioning properly

Within my Next.js project, I have successfully implemented a custom hook and component. The functionality works smoothly as each section opens independently without interfering with others, which is great. However, there are two issues that I am facing. Fi ...