Revamp your arrays with input fields in Vue3

When presented with two arrays of options, users must select an option from each array.

==> first array: [ orange, green, yellow ] ==> second array: [ orange, green, yellow ]

The challenge is to update the second array based on the user's selection in the first array. For example, if a user selects "green" in the first input:

==> first array: "green" (selected) ==> second array: [orange, yellow] (options not selected in the first array)

I am new to Vue3 and unsure how to achieve this. Any help would be appreciated!

Answer №1

If you want to filter out remaining items using computed property, here's how you can do it:

const { ref, computed } = Vue
const app = Vue.createApp({
  // COMPOSITION API
  setup() {
    const items = ref([ 'orange', 'green', 'yellow' ])
    const selected = ref(null)
    const selected2 = ref(null)
    
    const remainItems = computed(() => items.value.filter(i => !i.includes(selected.value)))
    
    return { items, selected, selected2, remainItems }
  }
  // OPTIONS API
  /*data() {
    return {
      items: [ 'orange', 'green', 'yellow' ],
      selected: null,
      selected2: null
    };
  },
  computed: {
    remainItems() {
      return this.items.filter(i => !i.includes(this.selected))
    }
  }*/
})
app.mount('#demo')
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ff9faeacfbca1bda1bdb6">[email protected]</a>/dist/vue.global.prod.js"></script>
<div id="demo">
  <div>Selected: {{ selected }}</div>
  <select v-model="selected">
    <option disabled value="">Please select one</option>
    <option v-for="item in items">{{item}}</option>
  </select>
  <div>Selected: {{ selected2 }}</div>
  <select v-model="selected2">
    <option disabled value="">Please select one</option>
    <option v-for="item in remainItems">{{item}}</option>
  </select>
</div>

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

How can I implement jQuery autocomplete with customized settings?

In my Drupal project, I am looking to implement jQuery's auto complete feature to search for nodes based on their titles. I am having trouble finding examples that align with my specific requirements: The URL structure should be: /api/node/title/{wh ...

Can you explain the distinction between $scope.$root and $rootScope?

When looking at controllers, I noticed that $scope includes $root. Can you explain what $root is and how it differs from the $rootScope that can be injected into the controller? ...

kineticjs equivalent to jquery sortable

Could someone recommend a kineticjs plugin or script that works with jquery's "sortable" function? I am trying to create a list of shapes where I can drag and drop them, and when one element moves, the other elements shift into place. ...

Looking for arrays with duplicate values across multiple elements - a comprehensive guide

I need assistance with a PHP script that retrieves arrays where the values of specific fields are identical. Below is the code I have so far, but as I am new to PHP development, any guidance would be appreciated. Code: foreach($leads_details->resul ...

Remove buttons from carousel elements in React Multi-Carousel

Is there a way to hide the arrows in the react-multi-carousel component? https://i.stack.imgur.com/V1nix.png ...

When utilizing the dojox.grid.enhanceGrid function to delete a row, the deletion will be reflected on the server side but

I have a grid called unitsGrid that is functioning correctly. I am able to add and delete rows, but the issue arises when I try to delete rows - they do not disappear from my unitsGrid. I have spent hours trying to debug the code but I cannot seem to fin ...

Substitute the information in the table with new data

I am working with an HTML table that has 5 columns, one of which contains dates. I need to convert the date format only if the data is not empty. To achieve this, I am utilizing moment.js for date formatting. While the date format conversion works perfect ...

Navigating through a list using tabs and automatic scrolling to a specific index in React with Material UI's Scrollspy

If there was a vast array of items, each belonging to a specific category, const categories: string[] = [0, 1, 2, 3, 4, 5]; const items: {name: string, category: number}[] = [{name: "foo", category: 1}, {name: "bar", category: 1}, {name ...

What is the best way to combine database table data based on two columns and then display it as a json

I am having some difficulty retrieving JSON data from PhpMyAdmin (MySQL). The desired format is: [{ “converterno”: ”1”, “zoneno”: {“1a”, “codeid”:[“t1”,”t2”,”t3”]}, “zoneno”: {“1b”, “codeid”:[“t21”,”t0”, ...

Ajax is incapable of achieving success or encountering failure

I'm having some trouble displaying search results on the view using AJAX. The action retrieves JSON data and sends it, but for some reason the AJAX call is not receiving the data. $(function () { $("#btnSearchForUser").click(function () { ...

Combine two arrays with different elements into a dictionary

I have a scenario where I need to combine two arrays containing HTML table data. The first array, $headers, will always have fewer elements than the second array, $data. Here's an example: $headers = array("Name","Phone","E-Mail"); $data = array("Bo ...

Using jQuery to send LocalStorage data to an email address with the help of PHP

Currently, I am in the process of developing a basic eCommerce/checkout system. My goal is to utilize localStorage data and transfer it to PHP using jQuery or any other method that is convenient. However, when I attempted to send the email, I only received ...

Switching between three div elements along with two icons can be done by using toggle functionality

Is there a way to make the icon change back when clicked again without making major changes to the existing code? Looking for suggestions on how to achieve this functionality. function myFunction() { var element = document.getElementById("demo"); el ...

Why is the button's ng-click changing the URL but not displaying the new view?

I recently started developing an IONIC application and encountered a challenge in navigating between pages upon clicking a button. Here is the progress I have made so far: In my index.html file, I have included various scripts and links for the applicatio ...

Warning: Update state in React-router-redux after redirection

I am currently developing an admin app for a project using react, redux, react-router, and react-router-redux. The version of react-router I am working with is v4.0.0, while react-router-redux is at v5.0.0-alpha.3 (installed with npm install react-router-r ...

Drop and drag to complete the missing pieces

Here is a drag and drop fill in the blanks code I created. The challenge I'm facing is that I want users to be able to correct their mistakes by moving words to the right place after receiving points. <!DOCTYPE HTML> <html> <head&g ...

Detecting changes to DOM elements without using jQueryResponding to DOM element

Suppose I have the following HTML structure: <div id='content'></div> I want to receive an alert when there are height mutations on this element. I thought about using the MutationObserver class for this, but I encountered a specifi ...

The scrolling speed of the mousewheel in Firefox is notably slower compared to that of Google Chrome

Kindly review this sample link: When I test the above example in Chrome and scroll using the mouse wheel, the page moves up by 100px each time. The Y position is displayed as well. However, if I try the same page in Firefox 26.0 and scroll with the mouse ...

When defining properties/data in Vue mixins, the properties/data of the mixin are not accessible

A vue mixin is being used to store information (referred as `world` in the example below) that needs to be accessed in multiple vue components without having to import it every time. Check out the code snippet: <template> <ol> <li> ...

Acquiring a fresh scope in Angular via a different component

In my project, I am developing an app using a component-based approach with Angular 1.5.5. As part of this development, I am utilizing d3js to create some elements with the class .floating-node. For each of these nodes, I am creating a new $scope and appe ...