Can Vue.js accommodate nesting a v-model within an array?

How can I implement filtering on groups within a Vue app using a nested array with v-model?

I attempted to achieve this using the following template...

<div id="app">
  <div class="filter__control filter__control--tags">
    <div class="filter__label">Colour</div>
    <div class="filter__list">
      <label><input type="checkbox" v-model="selectedTags[0]" value="Harvest">Harvest</label>
      <label><input type="checkbox" v-model="selectedTags[0]" value="Moss">Moss</label>
      <label><input type="checkbox" v-model="selectedTags[0]" value="Navy">Navy</label>
      <label><input type="checkbox" v-model="selectedTags[0]" value="White">White</label>
    </div>
  </div>

  <div class="filter__control filter__control--tags">
    <div class="filter__label">Size</div>
    <div class="filter__list">
      <label><input type="checkbox" v-model="selectedTags[1]" value="L">L</label>
      <label><input type="checkbox" v-model="selectedTags[1]" value="M">M</label>
      <label><input type="checkbox" v-model="selectedTags[1]" value="S">S</label>
      <label><input type="checkbox" v-model="selectedTags[1]" value="XL">XL</label>
      <label><input type="checkbox" v-model="selectedTags[1]" value="XS">XS</label>
    </div>
  </div>
</div>

Here is the vue instance configuration...

var app = new Vue({
  el: '#app',
  data: {
    selectedTags: []
  },
  watch: {
    selectedTags: function() {
      // Is this the expected format for the selected tags array?
      this.selectedTags = [
        ["Navy"],
        ["XS", "S"]
      ]
    }
  }
});

Answer №1

Instead of using a watcher, you can simply assign them to separate parameters of your SelectedTags object.

var app = new Vue({
  el: '#app',
  data: {
    selectedTags: {
      color: [],
      size: [],
    }
  },
  
  // If you require a specific format for the tags, you can utilize a computed property.
  
  computed: {
    SelectedTagsArrays: function(){
      return [this.selectedTags.color, this.selectedTags.size];
      }
    }
});
<div id="app">
  <div class="filter__control filter__control--tags">
    <div class="filter__label">Colour</div>
    <div class="filter__list">
      <label><input type="checkbox" v-model="selectedTags.color" value="Harvest">Harvest</label>
      <label><input type="checkbox" v-model="selectedTags.color" value="Moss">Moss</label>
      <label><input type="checkbox" v-model="selectedTags.color" value="Navy">Navy</label>
      <label><input type="checkbox" v-model="selectedTags.color" value="White">White</label>
    </div>
  </div>

  <div class="filter__control filter__control--tags">
    <div class="filter__label">Size</div>
    <div class="filter__list">
      <label><input type="checkbox" v-model="selectedTags.size" value="L">L</label>
      <label><input type="checkbox" v-model="selectedTags.size" value="M">M</label>
      <label><input type="checkbox" v-model="selectedTags.size" value="S">S</label>
      <label><input type="checkbox" v-model="selectedTags.size" value="XL">XL</label>
      <label><input type="checkbox" v-model="selectedTags.size" value="XS">XS</label>
    </div>
  </div>
</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

This issue with ng-include not functioning properly in Chrome but working fine in Firefox

My code is running only in Firefox and not working in Chrome. HTML Code <div ng-controller="myController1"> Select View: <select ng-model="employeeview"> <option value="1.html">1</option> < ...

Activate a jQuery plugin on elements that are dynamically generated

Here is the code snippet I am using: $(".address-geocomplete").geocomplete({ country: "US", details: ".details", detailsAttribute: "data-geo" }); When these elements are loaded with the document, everything works smoothly. However, there seem ...

A technique to trigger the display of a detailed grid by clicking in the master section

Currently, I am in the process of creating a reusable master-detail grid component using Backbone. My goal is to incorporate an event that triggers the loading of the detail grid whenever a user clicks on a row within the master grid. However, I am facin ...

How to access previous Redux state when navigating back using the Back button

Imagine a search page equipped with a search bar and various filters for the user to customize their data retrieval. As the user selects different filters, an API call is made to fetch updated data and the route path is also adjusted accordingly. However ...

Using TypeScript path aliases to resolve import errors

After creating a Vue.js project using Vue CLI 3 and setting up the import statement with the @ alias, I encountered an error when running npm run build. Can you explain why this happened? Error Message $ npm run build > [email protected] build / ...

Implementing a translucent overlay onto a specific HTML section using sidebar.js/jQuery

Searching for a way to enhance the functionality of my website using Sidebar.js, I came across an interesting feature on hypebeast.com. When you click on the three-bar icon, the main container section's opacity changes. How can I achieve this effect? ...

The function within the React component is failing to produce the expected output

After importing two types of images (IMG and IMG2), I wanted to display IMG when the viewport width is less than 600px, and IMG2 when it's equal to or greater than 600px. I created a function to determine the viewport width and return the respective i ...

Generating dynamic slots in VueJS allows for the creation of

Creating slots dynamically from an array is my current task. After some tinkering, I've managed to make it work using the following code snippet: <template v-for="(department,id) in departments" v-slot:[id]="record"> < ...

When attempting to execute a promise within a test, encountering a 400 error in a NodeJS environment

I recently started using Contentful, a new JavaScript library for creating static websites. My goal is to incorporate it into my Node.js project. To achieve this, I developed an app file called getContent.js: 'use strict'; var contentful = req ...

Using Vue js to scrollIntoView will only affect the specific element's div

I am currently working on a Vue page that consists of two divs. The Select-Div contains a list of elements, and when I click on an element from the list, the Scroll-Div is supposed to scroll to the corresponding ID element inside it. Ideally, the scrolling ...

modify the final attribute's value

Hello I've been using skrollr js to create a parallax website. However, the issue arises when determining the section height, as it depends on the content within. My goal is to locate the last attribute and adjust the number value based on the section ...

Activate a function for a targeted ajax request within a global $.ajax event

I have a series of 3-4 ajax calls that are scheduled to be executed at some point. In one of these calls, I want to activate a function on the global ajaxSend event. This particular ajax call may not be the first or last in the sequence. However, when I at ...

Guide on how to programmatically assign a selected value to an answer using Inquirer

Currently, I'm utilizing inquirer to prompt a question to my users via the terminal: var inquirer = require('inquirer'); var question = { name: 'name', message: '', validation: function(){ ... } filter: function( ...

Activate collapsible navigation icon when clicked on a smaller screen

Seeking assistance as a newcomer to coding. Struggling to implement a responsive navbar icon that changes on small screens when clicked. Utilized a bootstrap navbar but encountering issues where clicking the navbar on a small screen only displays the list ...

Issue with Laravel 5.7 Autocomplete search: JavaScript unable to recognize the specified route

I've been following a tutorial on this video: https://www.youtube.com/watch?v=D4ny-CboZC0 After completing all the steps, I encountered an error in the console during testing: jquery.min.js:2 POST http://apr2.test/admin/posts/%7B%7B%20('autocom ...

What are the steps to incorporate an iframe containing uniquely designed XML content?

I'm currently working on a website project for a client who specifically wants to include news from the BBC. Despite spending 3 hours searching online, I haven't been able to successfully insert an XML file into an iframe and style it to the clie ...

Mastering the art of customizing modal input fields in Vue.js

I am currently working on a task manager app and I'm facing a problem. I want to be able to edit an existing task, but when I use v-model, the task edits instantly without the need for a save button. However, I do not want this behavior. I would like ...

Even after hitting ctrl+z, Node.js continues to listen on the port

I've been exploring some features of Node on Ubuntu, and I encountered an issue where even after pressing ctrl+z in my console to stop the server (to update my code changes), Node would still be listening on port 3000. This meant that every time I nee ...

Preventing users from inputting the symbols "+" or "-" in a React JS input field

Essentially, the input field should only accept values between 1 and 999 Input Field : <input type="number" value={value} onChange={this.props.onViltMaxUserChange} min="0" max="999" /> onChange : onViltMaxUserChange = _.throttle(e = ...

`vue-router refusing to push when within a store`

This is the store I am using: import Vue from 'vue' import Vuex from 'vuex' import router from '@/main'; import mainStore from './modules/main-store'; import loginStore from './modules/login-store'; Vue.u ...