Passing the value emitted from the vue-multiselect.js component to the main view

I am trying to pass data from my MultiSelectFilter component to my Home.vue view, but I am not seeing any results when using console.log. Even after checking the documentation on using the @input event for capturing changes in selection (), I can't seem to get it to work properly.

As a newcomer to vueJS, I'm struggling to identify where I might be going wrong. Additionally, I have attempted to pre-select all default values without success in changing the value upon initialization.

My main objective is to create multiple selectors within my view by looping through them, which led me to develop a component for this purpose.

Below is a snippet from my Home.vue file:

<template>
    <main id="Home-page">
        <h1>Dashboard</h1>
        <div class="filter-wrapper">
            <div class="filter-icon">
                <font-awesome-icon icon="fa-solid fa-filter" />
            </div>
            <div class="filter-content">
                <MultiSelectFilter :options="teamOptions" label="teams"/>
            </div>
        </div>
    </main>
</template>

... // Rest of the code can remain as is

This is how I structured my MultiSelectFilter.vue file:

<template>
  <multiselect v-model="selectedOptions" :select-group-label="selectGroupLabel"
    group-values="data" group-label="all" :group-select="true" :options="transformedOptions" :placeholder="label"
    :multiple="true" :searchable="searchable" :close-on-select="false" @input="updateSelectedOptions">
    <template v-slot:noResult>{{ noResult }}</template>
  </multiselect>
</template>

... // Rest of the code can remain as is

Answer №1

Make sure to listen for the event in the parent component.

Check out the official documentation for more information: https://vuejs.org/guide/components/events.html

To do this, add

@select="updateSelectedOptions"

Home.vue

<template>
    <main id="Home-page">
        <h1>Dashboard</h1>
        <div class="filter-wrapper">
            <div class="filter-icon">
                <font-awesome-icon icon="fa-solid fa-filter" />
            </div>
            <div class="filter-content">
                <MultiSelectFilter :options="teamOptions" label="teams" @select="updateSelectedOptions"/>
            </div>
        </div>
    </main>
</template>

MultiSelectFilter.vue

methods: {
  updateSelectedOptions(selectedOptions) {
    this.selectedOptions = selectedOptions;
    this.$emit('select', selectedOptions);
  },
}

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

Checking for Click Events in React Components

Recently, I created a React component named HelpButton with the following structure: import helpLogo from '../Resources/helplogo.svg'; function HelpButton(props) { const [isOpen, setisOpen] = React.useState(false) function toggle() { ...

Utilize the useRef hook to dynamically retrieve the updated height when children are altered

I am working with an accordion component and using the useRef hook to measure the height of the children. However, I noticed that when I update the content of the children dynamically, the height measurement does not get updated unless I click on the toggl ...

Store Dark Mode preferences in the browser's local storage using React and Material-UI

Is there a way to save the dark mode setting in localStorage? Can this approach be used for storing it? Any suggestions on how to achieve this would be appreciated. Thanks, cheers! function App() { const [darkState, setDarkState] = useState("&qu ...

Extracting data from properties within a Vue.js local component

Within my Vue Instance, I have two local components that receive props from the data of the Vue Instance. However, when attempting to access the values of these props in one of the local components, they appear as undefined. The code snippet is as follows ...

Is there a way to create triangles on all four corners of an image using canvas in JavaScript?

In my code, I am currently working on drawing rectangles on the corners of an image using JavaScript. To achieve this, I first extract the image data from a canvas element. // Get image data let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height) ...

Vue's watch feature will not execute if the watched property remains unchanged during a page reload

<template> <!-- The navbar has a property for gender, the color of the navbar will change depending on the gender --> <!-- pass the props gender through a form submission and event --> <div class="nav-main" :class="{f ...

divide the JSON object into distinct indexes

Below is an array containing JSON objects: items = [ { id: '1', name: 'Josh', transactionDate: '2012-08-10', creditAmount: '200', numberBank: '1234 ...

Having trouble displaying the Primevue dialog modal in Vue 3?

I am using [email protected] and [email protected] Main script import { createApp } from 'vue' import App from './App.vue' import router from './router' import PrimeVue from 'primevue/config'; import &apos ...

activate the button once valid input has been provided

How can I enable a button based on the amount entered? Let's say there is a minimum of 100 and a maximum of 200. If the user enters an amount below 100, display an error message and keep the button disabled. If the user enters an amount above 200, ...

Quasar checkboxes for a multiple selection menu

I am working on creating a multi-select menu with checkbox options that is also filterable. You can check out a functional example provided by @Houssem here: https://codepen.io/HoussemDbira/pen/zYEgVmb The issue arises when trying to use v-model inside sc ...

Issue with Vue multiselect preventing creation of new tags

Having a functional Vue multiselect feature that utilizes an autocomplete function via an axios call to a database. The process of retrieving DB results, adding them as options, and turning them into tags works seamlessly. The main issue arises when tryin ...

What is the best way to send JSON data from Express to a JavaScript/jQuery script within a Pug template?

Currently, I am facing a challenge in passing JSON data from an Express route to a .js file located within a .pug template. I have been attempting to solve this issue using the following method: The router: // Office Locations router.get('/office_lo ...

How to automatically adjust select view in AngularJS as model value is updated

My select element is structured as follows: <select data-ng-model="transaction.category" class="form-control" data-ng-options="category.name group by category.parent for category in categories" required> </ ...

Incorporate a header token into axios requests following a successful login action within vuex

I have successfully built a login system using Laravel Passport, but I am facing an issue when trying to add the header token to Axios requests. I have included the following code in my ProjectEmploye.vue file: created(){ axios.defaults.headers.common[" ...

Sending a parameter to a confidential npm module

I've developed a custom npm module that can be used in my applications by including this code snippet in the HTML: <app-header-name></app-header-name> Here is the TypeScript code inside the npm package: import { Component, OnInit } from & ...

Error: The index "id" is not defined

Hey there, I have been working on fetching data from a JSON URL located at this link. However, I seem to be encountering an error that I can't quite comprehend. If anyone has any insights or solutions, I would greatly appreciate the help. Thank you in ...

Sending multiple arguments to a Vuex action

In the Vue Component code snippet below, I have a method: loadMaintenances (query = {}) { this.getContractorMaintenances(this.urlWithPage, query).then((response) => { this.lastPage = response.data.meta.last_page }) } I am trying to pass the par ...

Tips for adjusting the up and down controls and spins of a date input after modifying its height

After adjusting the height of my inputs (date type) to 40px, I noticed that the up and down controls are no longer aligned with the text inside the field. I am looking for a solution to either adjust the height of the spins or remove them if necessary in ...

What is the best way to save JavaScript array information in a database?

Currently working on developing an ecommerce website. My goal is to have two select tags, where the options in the second tag are dynamically populated based on the selection made in the first tag. For example, if "Electronics" is chosen in the first tag f ...

Associate an alternate attribute that is not displayed in the HTML component

Imagine there is a collection of objects like - var options = [{ id: "1", name: "option1" }, { id: "2", name: "option2" } ]; The following code snippet is used to search through the list of options and assign the selected option to anot ...