Activate loading state when input changes with VueJS and Vuetify by using @change attribute and setting loading="true"

I am attempting to develop a function that is activated by the @change event on multiple input fields.

My goal is to set loading="true" for the targeted input until the axios request (PATCH) is completed.

PLEASE NOTE:

There are several v-select components on the page, and I only want to set loading=true for the specific target - the current input/select.


EXAMPLE:

When a user selects an option in the first select in the following example, the inlineUpdate function is triggered, which changes the target to appear like this

<v-select loading="true" @change="$root.inlineUpdate" ...

Subsequently, once the update is completed, the loading attribute is removed.


I believe I need to pass an event argument into the method, but it is not functioning as expected.

Currently, only a value is passed, and it is unclear which input should be marked as loading.

 <v-select @change="$root.inlineUpdate" ...
 <v-select @change="$root.inlineUpdate" ...
 <v-select @change="$root.inlineUpdate" ...
 <v-text-field @change="$root.inlineUpdate"...

 inlineUpdate(event){
              console.log(event)
              console.log(event.target)
             # here I need to set loading="true" for the input
             axios.post(...).finally(() => { // remove the loading })
             # stop loading
            },

console logs show:

>>> house
>>> undefined

Is there a solution to this issue?

Answer №1

To ensure smooth loading specifically for the v-select, it is important to create a dedicated variable for handling the loading state. This variable, named isSelectLoading, is initially set to false. During the API call process, it is toggled to true, and upon the resolution of the promise, it is set back to false.

<v-select label="Typ nehnuteľnosti" :loading="isSelectLoading" @change="$root.inlineUpdate" ...

...
data: () => ({
isSelectLoading: false
}),
methods: {
inlineUpdate(){
this.isSelectLoading = true;
axios.put("/some/url", ...)
.then(apiResponse => { //do some stuff })
.catch(apiError => { //oh no! })
.finally(() => { this.isSelectLoading = false });
},
}

Enhancing for multiple v-selects

Managing loading state for multiple v-select components requires an array named isSelectLoading. This array, initially holding false values, keeps track of loading states for each v-select.

Additionally, to differentiate between the v-select elements being altered, a variable named activeSelect is added. This variable is updated using the @click event listener on the respective v-select.

By linking each v-select to its loading state within the array, only the triggered v-select will display loading status when its selection is modified.

<v-select 
@change="inlineUpdate" 
@click="activeSelect = 0"
:loading="isSelectLoading[0]"...
<v-select 
@change="inlineUpdate" 
@click="activeSelect = 1"
:loading="isSelectLoading[1]"...
<v-select 
@change="inlineUpdate" 
@click="activeSelect = 2"
:loading="isSelectLoading[2]"...
<v-text-field
@change="inlineUpdate" 
@click="activeSelect = 3"
:loading="isSelectLoading[3]"...

...
data: () => ({
activeSelect: null,
items: ["a", "b", "c"],
isSelectLoading: [false, false, false]
}),
methods: {
inlineUpdate(){
this.toggleLoading(this.activeSelect);
axios.put("/some/url", ...)
.then(apiResponse => { //do some stuff })
.catch(apiError => { //oh no! })
.finally(() => { this.toggleLoading(this.activeSelect); });
},
toggleLoading(index) {
this.isSelectLoading[index] = !this.isSelectLoading[index]
}

While the example simplifies the concept, in practical scenarios, these elements could be efficiently managed using a

<template v-for="(item, index) in selects">...
structure. Here, selects would provide the necessary fields/properties for generating select components, replacing hardcoded indexes with the index value for event bindings.

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

The React Fabric TextField feature switches to a read-only mode once the value property is included

I've been grappling with how to manage value changes in React Fabric TextFields. Each time I set the value property, the component goes into read-only mode. When utilizing the defaultValue property, everything functions correctly, but I require this i ...

In the world of Node.js and Java, the concepts of "if"

Here is a code snippet that I am working with: var randFriend = friendList[Math.floor(Math.random() * friendList.length)]; if (randFriend == admin) { //Do something here } else if (randFriend != admin) { client.removeFriend(randFriend); } I am tr ...

The Filereader seems to be having trouble reading a specific file

In my Next.js application, I am attempting to allow users to upload an image from their system using an input field of type "file" and then read the content of the file using FileReader(). const OnChange = (e) => { const file = e.target.files[0]; ...

Error SCRIPT438: Property or method not supported by object in Internet Explorer

Within my application, there is an option for users to deactivate their profiles, which can only be reactivated by the admin. I have created a class called ActivateProfile with two methods: userExist(userName) to check if a user with the given userName ...

Node.js: The object in req is not functioning as expected

// Function to merge objects with unsent columns kept from old objects. function merge_options(obj1, obj2) { const obj3 = {}; for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; } for (var attrname in obj2) { obj3[attrname] = obj2[attrname] ...

Utilizing JavaScript to dynamically resize an element within a slide as soon as the slider transitions to the following slide

RESOLVED! ISSUE FIXED! While working on my personal website using WordPress and the Smart Slider 3 plugin, I encountered a problem with the positioning and size of an element in the second slide. Despite finding a tutorial explaining how to manually trigg ...

Error 404 encountered while trying to access a website with parameters using Vue.js

Currently, I am in the process of building a website using VueJS and recently discovered how to use url parameters. Everything was working perfectly on my local machine - I could easily navigate to different pages by including parameters in the URL. For e ...

Incorporating Axios data into a React component

I am seeking assistance with React development as I am facing challenges in mapping the data retrieved from a simple API call using Axios. This problem has been troubling me for a few days now. Below, you will find my App.js App Component along with the AP ...

Steps for displaying a particular slide from a thumbnail in a modal with Slick Carousel

My setup includes an array of thumbnails and a Slick Carousel within a hidden modal. Is there a way to trigger the opening of the carousel by clicking on a thumbnail, ensuring that it starts from the corresponding slide? https://i.sstatic.net/BJU2o.png ...

What could be the reason for the malfunction of jQuery's show() function?

Using jQuery, I have implemented a functionality to hide a div using the hide() method. However, upon clicking a link, the div is supposed to show but unexpectedly disappears after appearing briefly. HTML Code Snippet <div id="introContent"> & ...

Divide the table row and store each cell using regular expressions

Here is the original source text: gi0/1 1G-Fiber -- -- -- -- Down -- -- Access gi0/2 1G-Fiber -- -- -- -- Down -- -- gi0/3 1G-Fiber -- -- -- -- Down -- -- gi0/4 1G-Fiber -- -- -- -- Down -- -- gi0/5 1G-Fiber -- -- -- -- Down -- -- gi0/0/1 1G-Fiber -- ...

If the width of the table is set to 100% in the CSS, the legend in the Flot chart will automatically shift to the

When the CSS for the table is set to { width:100%}, the Flot chart's legend moves to the left side. Is there any way to maintain the table { width:100%} while also preventing this shift, considering that the CSS is applied site-wide? Here is a jsfid ...

Retrieve the latest inserted ID in a Node.js application and use it as a parameter in a subsequent query

I am currently working with an SQL database that consists of two tables, namely club and players. These tables are connected through a one-to-many relationship. Although the query in my node.js code is functioning properly, I am facing an issue retrieving ...

Having trouble retrieving the API URL id from a different API data source

For a small React project I was working on, I encountered a scenario where I needed to utilize an ID from one API call in subsequent API calls. Although I had access to the data from the initial call, I struggled with incorporating it into the second call. ...

Use jQuery to switch back and forth between the login and registration forms on a single

I've set up two forms, one for login and one for registration, with the default view showing the login form. There's a link that says "Don't have an account?" and when it's clicked, the registration form will display while the login for ...

Verify that the computer is connected to the Internet by sending an ajax request to Google

Whenever I need to test my internet connection, I rely on the following code snippet: const checkInternetConnection = () => { $('input').ajaxError(function(){ alert("failed"); }); $.get('http://www.google.com', f ...

Issues with jQuery Ajax sending Post data to PHP script

I've been attempting to transfer variables from my JavaScript to a PHP file using AJAX, but for some reason, it's not functioning as expected. Despite researching numerous similar queries, I have yet to come across a solution. Here is an excerpt ...

What is the best way to resize my hamburger menu and achieve a flawlessly circular border for it?

I've been struggling to reduce the size of my hamburger menu (both height and width) for some time now. I managed to make it a bit smaller, but I can't seem to figure out how to shrink it further. Additionally, I'm having trouble creating a ...

Utilizing the Command Line/Window feature within Visual Studio Code

As a newcomer to Visual Studio Code, I'm currently using the latest Version: 1.29.1. When I used Matlab, I had access to a script window for writing code, a Command Window for testing code snippets and viewing variable values, as well as a workspace ...

Updating a single .jshintrc option for a folder

My project has a .jshintrc file at the root, containing the following settings: { "node": true, "smarttabs": true, "undef": true, "unused": true } While these settings work well for node-related code in my project, they are not suitable for brows ...