Utilizing Vue.js to dynamically populate one multiple select based on another multiple select's

Can I create two multiple select fields where the options in the second select menu are based on what is chosen in the first select?

Clarification:

Each category has various associated descriptions.

The initial select menu, named "categories", contains static options that do not change. The second select menu is known as "descriptions".

I can choose multiple categories. If I select a category from the first menu, the second menu will display all descriptions linked to that category as options. Whenever I choose another category, the related descriptions will be appended to the second menu, and vice versa when deselecting categories. I want it to update dynamically.

I have come across these resources:

https://github.com/sagalbot/vue-select https://github.com/monterail/vue-multiselect

However, I am still searching for a solution that accommodates two multiple selects together. Any suggestions?

P.S. The number of categories and descriptions is minimal, so loading them all into the view for Vue manipulation is feasible without requiring an ajax call.

Answer №1

One way to achieve dynamic population of the second select is by updating its data source based on events triggered by changes in the first select.

Here's a simple outline to illustrate this concept:

<template>
  <div class="root">
    <!-- initial select with hardcoded options -->
    <select v-on:change="populate($event)" multiple="multiple">
      <option value="1">One</option>
      <option value="2">Two</option>
    </select>
    <select>
      <!-- reactive variable for options -->
      <option v-for="option in options" :value="option.value">{{option.text}}</option>
    </select>
  </div>
</template>

<script>
export default {
  name: 'Selects',
  data () {
    return {
      options: []
    }
  },
  methods: {
    populate (event) {
      const options = []
      
      Array.from(event.target.selectedOptions).forEach(item => {
        options.push({
          value: item.value,
          text: `You have selected ${item.text}`
        })
      })

      this.options = options
    }
  }
}
</script>

Update:

Check out the jsfiddle example here -> https://jsfiddle.net/bpgp11da/

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

A guide on showcasing Firebase array elements in Vue.js

I have developed a Vue.js application that allows users to add items to an array stored in a Firebase document. The function responsible for adding items to the array in the database is functioning correctly. However, I am facing an issue where the entire ...

Using AngularJS to access form field ids that are generated dynamically

I am dynamically generating form fields using ng-repeat and everything is functioning correctly. However, I now want to incorporate an angular datepicker component that is based on a directive. The issue I am facing is that it only seems to work with stat ...

Chrome and FireFox Encounter Ajax Functionality Issues

This script that I have created works flawlessly on Internet Explorer! However, it encounters a few issues when used on Chrome and Firefox. Specifically, it only functions correctly for the first action performed, but fails to do so for subsequent actions. ...

angular: handling duplicates in ng-repeat

Here is the JSON data: streams = [{ id: 0, codec_type: 'video', content: '1920x1040 - H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10' }, { id: 1, codec_type: 'audio', content: '5.1(side) - cze - undefined' }, ...

Configuring webpack: Eliminate hash in the file names of entry/bundle *.js files

I'm currently working on a Vue.js app. However, after building it, instead of getting the "background.js" file, I end up with "background.2a548437.js". To configure webpack-chain, I am using the "vue.config.js" file. While inspecting the results of ...

Prevent jQuery scrollTop from updating the browser history

Is there a way to prevent scrollTop from creating browser history records? Upon launching the page, I want the browser to automatically navigate to a specific section if the URL includes a parameter with an internal anchor name. To achieve this, I use the ...

Include the data-title attribute within the <td> elements of my datatables

To apply the magic of jQuery datatables to all my tables, I use datatables. For responsive tables, I add data-title to my td's. Is there a way to automatically add data-title to all my td's like below? <td data-title="Fruit">Apple</td&g ...

Creating a vendor bundle in create-react-appReady to optimize your create-react

When using the create-react-app tool, how can you specifically create a separate vendor bundle? While code splitting can be achieved easily with the react-code-splitting package, I have not been able to find clear instructions on generating vendor bundles ...

Mirror the input text to another input within a for loop

I have a list of questions displayed, each with an input field for entering a phone number. How can I use jQuery or JavaScript in a for-loop to mirror the text entered in the first phone input box to all subsequent phone input boxes? ..Enter your phone... ...

Continuously encountering the "Uncaught Error: Bootstrap dropdown requires Popper.js" message despite having already added popper.js to the code

Recently beginning my journey with Angular and Bootstrap, I decided to create a simple "hello world" app. I've included all the necessary libraries, but I encountered an error that has me stuck. Error: Bootstrap dropdown requires Popper.js I' ...

Tips for extracting a value from a currently active list item's anchor tag with JQuery on Mapbox API?

Currently, I am attempting to extract the value from a forward geocoder that predicts addresses while a user is typing. My goal is to then send this value to a form with an id of "pickup". However, I am encountering difficulties in capturing the li > a ...

Tips for eliminating additional white space within a bootstrap row

I'm having trouble removing the extra space on my website while using Bootstrap 5. I've tried various Bootstrap classes like pr-0, mr-0, p-auto, m-auto but none of them seem to work. I also attempted using CSS margin-right: 0; but that didn' ...

A comparison of Vue's import paths: exploring the variations

Exploring the world of Vue has been exciting but I'm a bit puzzled by the syntax used for importing different libraries. Take, for instance, this import statement: import Vue from 'vue'; import axios from 'axios'; Where do these r ...

Combining arrays of objects in JavaScript

I am currently working on combining different arrays: const info1 = {id: 1} const info2 = {id: 2} const info3 = {id: 3} const array1 = [info1, info2] const array2 = [info1, info3] const array3 = [info2, info3] const union = [...new Set([...array1, ...arr ...

What is the most effective method to patiently anticipate a specific duration for a function's output?

I am faced with a situation where I have two functions at hand. One function performs complex logic, while the other wraps this function to provide either the result of computation or an error message after a specified amount of time t. Consider the follo ...

Troubleshooting JavaScript Functions Failure Following AJAX XML Request

My current project involves developing an interactive map where users can select a year from the timeline and apply filters. This functionality is achieved through XML HttpRequest, which refreshes the SVG each time a selection is made. The SVG code for th ...

What is the best way to prevent an HTML form from being submitted when a user is not logged in, but still allow submission when the user is signed

One of the requirements for users of my application is to be signed in before they can submit form information. Upon clicking on the submit button, my jQuery script verifies if the user is already signed in. If the user is not signed in, an error message ...

arrange data based on table heading using vue.js

I've recently upgraded to Vue 2.x and found out that the orderby directive is no longer supported as per this documentation. I'm trying to avoid adding another JavaScript library, but if using lodash is the only option, I'm willing to give ...

A Step-by-Step Guide to Retrieving the Route of a Controller Function in Express

When working with Express.js, it is possible to retrieve the names of all controllers from an app. However, these names are usually in an unfamiliar format (such as handle: [AsyncFunction: login]). I have been unable to figure out how to destructure this i ...

What is the best way to adjust the size of a Div slideshow using

I need help with creating a slideshow that covers my webpage width 100% and height 500px. The image resolution is 1200*575. Can someone assist me with this? CSS #slide{ width : 100%; height: 500px; } HTML <!DOCTYPE html> <html> ...