Utilize Vue JS to trigger a query when an option is selected in a drop-down select

I'm new to Vue and facing a challenge. I have multiple drop-down select menus that fetch data from an Axios request. The select only responds when I click the submit button. I want it to update when the user makes a selection from the drop-down. I can't figure out what I'm missing. Can someone assist?

Here's a snippet of the code. Thank you

The HTML:

 <div class="form-group col-md-6">
                      <label for="subject" class="control-label">Subject</label>
                      <select
                        v-model="subject"
                        class="form-control"
                        :disabled="selectDisabledSubject"
                      >
                        <option disabled value="">Select subject</option>
                        <option
                          v-for="subject in uniqueSubjects"
                          :key="subject"
                          :value="subject"
                        >
                          {{ subject }}
                        </option>
                      </select>
                    </div>

The Vue:

uniqueSubjects() {
      const metaVFees = this.results
        .filter((result) => result.metaData && result.metaData.V)
        .map((item) => item.metaData.V)
        .filter((subject, i, arr) => arr.indexOf(subject) === i);
      // Split multiple subjects in strings and store in an array
      let subjects = [];
      metaVFees.forEach((item) => {
        const splitArr = item.split(", ");
        subjects = subjects.concat(splitArr);
      });
      return subjects
        .sort()
        .filter((subjects, i, arr) => arr.indexOf(subjects) === i);
    },

Answer №1

Vue allows you to utilize Watchers for dynamic updates.

watch:{
 subject(newVal){
  // Invoke the necessary function on submit
 }
}

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

Is there a way to display various data with an onClick event without overwriting the existing render?

In the process of developing a text-based RPG using React/Context API/UseReducer, I wanted to hone my skills with useState in order to showcase objects from an onclick event. So far, I've succeeded in displaying an object from an array based on button ...

The mistake in npm install is when the console logs a bug that is notorious for causing npm to malfunction

After reading this article, I successfully installed Node.JS version 9.4.0. $brew install node $node -v $v0.12.7 Next, I ran npm install -g grunt-cli for testing. H ...

Having trouble loading select fields using AJAX within Wordpress? Update and find a solution!

UPDATE: I am struggling to iterate through the response object and populate a select field using my ajax function. Although I have tried using a for loop instead of each(), the select field gets populated with "undefined". Any suggestions on how to resolve ...

Display the keys of a nested array in Vue.js when the structure is unknown

Here is a representation of a dynamic array I have: nodes: [ { n1: "Foods", }, { n4: "Drinks", b7: [ { a2: "Beers", a4: [ ...

before the ajax response is received, running ahead

I've encountered an issue where I'm attempting to execute a function after an ajax return has finished using then, but it appears to be running before the return completes. Here is the code snippet in question: var existingUser = false; $.ajax( ...

Exporting stylesheets in React allows developers to separate

I am trying to figure out how to create an external stylesheet using MaterialUI's 'makeStyles' and 'createStyles', similar to what can be done in React Native. I'm not sure where to start with this. export const useStyles = m ...

What is the best way to utilize Typescript when making queries to Firebase?

I have successfully integrated push notifications for my app using Firebase Cloud Functions. Now, I am looking to enhance the user experience by updating the app's badge count alongside the push notifications. I understand that this can only be achiev ...

Guide to attaching a page content button to a fixed header

I am currently developing a webpage with a sticky header, and I want to include an animated button that moves onto the header when scrolled past. You can check out my code snippet here: https://jsfiddle.net/ykL50pjf/ The specific ID for the button is #t ...

Center the text within the div and have it expand outwards from the middle if there is an abundance of text

I have a div that is in the shape of a square box and I want it to be centered. No matter how much text is inside, I would like it to remain in the middle. I am using JQuery to create my square box and would like to center it using CSS. Here is my code: &l ...

The npm script isn't executing

In my package.json file, I have the following script: "scripts": { "build": "browserify app/components/main.js -o build/js/app.js -d" } Executing this command from the shell works fine and the file is created successfully. But when I try to run npm bui ...

Tips for determining if an array includes a specific item

I am seeking to limit the keys that are allowed to be pressed. $('.txtComments').keydown(function (event) { var keys = new Array(); keys[0] = "8"; keys[1] = "46"; keys[2] = "37" keys[3] = "39" if(!(....)) //Verify if the keyCode cor ...

Modify the CSS properties of the asp:AutoCompleteExtender using JavaScript

Is there a way to dynamically change the CompletionListItemCssClass attribute of an asp:AutoCompleteExtender using JavaScript every time the index of a combobox is changed? Here is the code snippet: ajaxtoolkit: <asp:AutoCompleteExtender ID="autocom" C ...

Passing reactive chart options in Vue.js using Chart.js

After carefully following the author's documentation, I was able to successfully create a reactive chart similar to the sample provided in the documentation. Below are snippets from both a JavaScript file and a Vue file: // Chart.js file import { ...

Tips for resolving the issue: 'Unhandled Promise Rejection: Error: Unable to resolve bare specifier "app.js" from http://localhost:3000/'

While delving into TypeScript, I have come across an error related to node modules. https://i.sstatic.net/IPx5A.png Upon clicking the anonymous function, it leads me to the following code snippet. https://i.sstatic.net/4U8dY.png <!DOCTYPE html> & ...

What is the best way to change a date from the format DD/MM/YYYY to YYYY-MM-DD

Is there a way to use regular expressions (regex) to convert a date string from DD/MM/YYYY format to YYYY-MM-DD format? ...

Displaying grid in object in threejs: a tutorial

I currently have a horizontal grid Helper and polygon within my scene. Is there a way to hide the grid outside of the polygon, similar to the image below? https://i.sstatic.net/pQ40h.png Here is the JSFiddle var camera, scene, renderer, controls, geo ...

Navigating between different pages on datatables with rowspans

I'm encountering a challenge in paginating a datatable with rowspans. The issue arises when setting the "pageLength" attribute, as it includes all trs. For instance, if I have a structure like this, I would need to specify "pageLength: 3". ----------- ...

How can I integrate a bug fix that has been resolved in the master branch but has not yet been deployed to npm into my project?

Our team is facing a situation where a crucial bug fix has been implemented in a library we are using, but the new version has not yet been released on npm. Can you suggest a solution for us to implement this change? ...

Dynamic Filtering with jQuery List

I'm attempting to create a dynamic filter list on keypress event. For example, if I type "It" into the input field, the elements that do not match this value will be hidden. I'm unsure if the code structure I have implemented below effectively ac ...

When making xmlhttp requests, IE9 will prioritize loading from the cache rather than from the server when the data is available

During my local development process, I've been utilizing this AJAX code: function getChart(num,ld,margin,idr) { idr = typeof(idr) != 'undefined' ? idr : 0; $(ld).style.display="inline-block"; if (window.XMLHttpRequest) { ...