How to fetch the option value in Vue.js and use it in the @click event

Hi there, I'm still learning vue js and could use some help. I'm trying to retrieve the value of 'option-value' in q-select by passing 'department_id' in my functions. I am currently using quasar. However, whenever I run this code, it displays as undefined. What is the correct way to pass and display the data?

<div class="col-3">
                <q-select 
                v-model="model" 
                :options="branches"
                multiple
                use-chips
                option-value="department_id"
                option-label="full_branchdept_name"
                label="Branch Department"
                class="select-branch-department"
                outlined dense />

            </div>
            <!-- :options="[]" -->
            <div class="col-9 q-mb-md">
                <!-- <div class="float-right">
                    <q-btn label="Generate from all device" class="bg-custom-orange text-capitalize text-white"></q-btn>
                </div> -->
                <div class="float-left">
                    <q-btn color="primary"
                    class="fetch-branch-department"
                    label="FETCH" 
                    @click="showFilteredEmployees(department_id)"
                    />
                </div>

Here's a snippet of my vue file:

methods: {
    showFilteredEmployees(department_id){
console.log(department_id); // when executed, it returns as undefined 
    }
}

Answer №1

Upon reviewing your code, it seems that you have opted for a multiple selection input. Hence, you will need to traverse your model to retrieve the selected department_ids.

Moreover, in the

@click="showFilteredEmployees(department_id)"
part, there is no necessity to pass the department_id or the model variable. It should simply be @click="showFilteredEmployees". Just bear in mind that your model variable will be accessible throughout the scope of your component and can be accessed through the this object.

An example of a functioning code could look like this:

<template>
  <div class="col-3">
    <q-select
      v-model="model"
      multiple
      use-chips
      :options="branches"
      option-value="department_id"
      option-label="full_branchdept_name"
      label="Branch Department"
      class="select-branch-department"
      outlined
      dense
    />
  </div>
  <div class="col-9 q-mb-md">
    <div class="float-left">
      <q-btn
        color="primary"
        class="fetch-branch-department"
        label="FETCH"
        @click="showFilteredEmployees"
      />
    </div>
  </div>
</template>

As for the method:

<script>
export default {
  name: "App",
  data() {
    return {
      model: [],
      branches: [
        { department_id: "1", full_branchdept_name: "Department 1" },
        { department_id: "2", full_branchdept_name: "Department 2" }
      ]
    };
  },
  methods: {
    showFilteredEmployees() {
      const filteredEmployees = this.model.map(employee => employee.department_id);
      console.log(filteredEmployees);
    }
  }
};
</script>

If you have any further questions, feel free to ask.

Answer №2

Although I am not familiar with the Quasar framework, based on your code it seems that department_id is the property in options that you want to use as the value for the select. In this case, you might consider updating

@click="showFilteredEmployees(department_id)"

to

@click="showFilteredEmployees(model)"

since model will represent the selected value in the q-select.

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

Vue.js Google Places Autocomplete Plugin

I'm currently working on integrating Google Places Autocomplete with Vue.js. According to the API documentation, the Autocomplete class requires an inputField:HTMLInputElement as its first parameter, like shown in their example: autocomplete = new g ...

Is it possible to use Node.js without resorting to template engines

Recently, I embarked on a journey to explore the world of backend development, focusing my attention on Node.js. As I delved into Express.js and familiarized myself with routing, a curious thought crossed my mind. Is there a method to transmit data direc ...

When there is no data in the request body, ExpressJS will display it as empty

A big part of my tech stack includes a website with an express server up and running. The website allows users to insert their username and password. Upon clicking the login button, the server receives a request with the username and password packed as a J ...

issue with angular promise not resolving properly

In my current project, I am trying to ensure that a promise resolves after making a query on Firebase. The goal is to retrieve specific keys from one table and then iterate through another table to fetch the desired artwork. artistFactory.js 'use st ...

I'm having trouble obtaining access to an HTML element within the useEffect hook

I am trying to create an itinerary control table using the element generated in <RoutingMachine />. However, I am facing issues accessing the HTML element in useEffect because it does not exist yet. const Routing = ({ pointA, pointB }) => { ...

Error: Authorization token is required

For email confirmation, I am utilizing JWT. An email is sent to the user with a URL containing the token. Here's an example of the URL received by the user: http://localhost:3000/firstlogin?acces_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI ...

Ways to avoid Parents Click Event from triggering during a Child's (Grandchild's) click event

My parent div has a unique feature where clicking on it reveals one of the child divs, and clicking again toggles the visibility of the child div. Inside this child div, there is a switch toggle that functions as a hidden checkbox. The issue I'm facin ...

Event with no refresh required

When using chat scripts, new lines can be added without reloading the page. The user's availability status can change without a postback. Similar to Facebook, a new comment can be added without reloading the page. How can an event be triggered in ASP. ...

Angular's onreadystatechange event is triggered when the state

Hey there, I'm new to Angular and had a question. Is it possible to use the $http service in Angular to trigger a function whenever there is any change in the ready state/status, not just for success or failure? If so, what would be the equivalent ang ...

Encountering unresponsive npm behavior post-prefix modification

Recently, I attempted to update my IONIC CLI via npm. The installation was successful multiple times, but the version of CLI remained unchanged. After some research, I decided to modify the npm prefix, which resulted in the IONIC command not being found. F ...

What is preventing me from filling my array with the URL inputted by the user?

I am looking to dynamically populate an array with URLs and display them one by one as users upload files. The goal is for each user to upload a file, have it stored in the array, and then displayed on the page. Specifically, I want to generate <img/&g ...

What is the best way to display prices for all days in a date range picker without having to use hover to show the price

Is there a way to display prices under the days without displaying "Thanks a lot"? Hey there, I'm currently utilizing this plugin and I've run into an issue regarding displaying prices when hovering over days. I was able to successfully show the ...

Transfer the information entered in one set of input fields to another set of input fields by copying

The copy button is designed to efficiently duplicate the text contained within the first input field. Once copied, clicking on the paste button will then insert this text into another designated area. <input type="text" id="txt1">Some text</inp ...

What is better - JSON response objects with descriptive keys and larger responses, or concise keys and smaller responses?

In my dynamic web application, I utilize ajax requests to fetch responses in JSON format. The returned data is typically an array of objects. Since the array often contains a large number of elements (even though the server gzip compresses the data), I o ...

Jquery is not working as expected

I am having trouble implementing a jQuery function to show and hide select components. It doesn't seem to be working correctly. Can someone help me identify the issue? <html> <head> <meta charset='UTF-8' /> <script ...

Submitting information from a single form to two separate URLs

Is it possible to send a post from one form to two different URLs simultaneously? For example, sending POST name data to both process.php and confirm.php at the same time. $(document).ready(function(){ $("#myform").validate({ debug: false, ...

How to switch between classes for elements and return to the original one when none of the elements is chosen

Hello there, I need some assistance. Here's the scenario: I am working with a grid of six items. My goal is to have the first item in the grid become active by default. When the user hovers over any of the remaining five items, I want the active clas ...

npm allows for multiple entry points for modules

I'm working on an NPM package and I'm curious about how to allow users to register multiple entry points. This way, they have the option to import either the entire library or just specific parts that they need. For instance, importing the compl ...

Is it possible to display different alert messages based on the language chosen?

Recently, I implemented a newsletter pop-up feature that allows users to sign up for a weekly newsletter. When a user tries to submit without entering their email address, an alert message pops up prompting them to do so. This is beneficial but now I wan ...

Is there a way in Angular Material to consistently display both the step values and a personalized description for each step?

Is there a way to display both numerical step values and corresponding custom text in Angular Material? I prefer having the number at the top and the descriptive text at the bottom. Check out this image for reference: https://i.stack.imgur.com/LGMIO.png ...