What is causing the toggle and filter feature to not function properly in Vue JS?

I am having trouble implementing a dropdown filter in my Vue.js project. I followed a tutorial and created a v-for directive, but when I try to filter the results using the dropdown, nothing happens. It seems like the name property value is not being assigned to the object correctly. I expect the filterList function to be triggered every time a new option is selected.

const clubs = [
    {
   name: 'Tigers',
   location: 'Manchester',
   members: '22',
   registered: 'No',
   pitch: 'Grass'
    },
    {
   name: 'Dolphins',
   location: 'Miami',
   members: '19',
   registered: 'Yes',
   pitch: 'Grass'
    },
    {
   name: 'Bleu Sox',
   location: 'Paris',
   members: '13',
   registered: 'Yes',
   pitch: 'Astroturf'   
    } 
];

const app = new Vue({
  el: '#app',
  data: {
    clubs,
    name: ''
  },
  methods: {
    toggleDetails: function(club) {
      this.$set(club, 'showDetails', !club.showDetails)
    },
    filterList: function() {
      this.name = event.target.value;
      console.log(this.name);
    }
  }
});

This is how my HTML looks:

<div id="app">

    <select v-on:change="filterList">
       <option v-for="club in clubs">{{club.name}}</option>
    </select>

  <ul>
    <li v-show="name === club.name" v-for="club in clubs" v-on:click="toggleDetails(club)">
      <h1>{{club.name}}</h1>
      <div v-show="club.showDetails">
          <p>{{club.location}}</p>
          <p>{{club.members}}</p>
      </div>
    </li>
  </ul>

</div>

Answer №1

No need to manually handle DOM events in this case - Vue's reactivity takes care of updating the name variable automatically when changes occur:

<select v-model="name">
   <option v-for="club in clubs">{{club.name}}</option>
</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

Can I relocate myself to the background in Node.js?

Although I am aware of this reference, it unfortunately doesn't address the specific question I have. I'm currently developing a Node.JS application to manage multiple Steam trade bots, and I'm in the process of adding a terminal-like inter ...

Testing a form submission in vue.js with Jest: a complete guide

Up until this point, I have been utilizing Avoriaz. However, I am now interested in using Jest. I have found some tutorials but haven't been able to find any guidance on testing my contact view component that sends a POST request to an external URL. ...

Use a forEach loop to transfer the content of one tag to another tag

EDIT: I'm confused as to why this was labeled as a duplicate when the previous question did not address my specific issue. Yes, it involves using querySelector, but how can I achieve this using forEach? Please help me revise this. I am attempting to ...

VueJS - vue2-editor rejecting certain tags

I have successfully integrated the vue2-editor, and here is the code snippet: <div id="app"> <vue-editor v-model="content"></vue-editor> <div v-html="content"></div> </div> </t ...

Tips for transferring information between two web pages using JavaScript

While I have come across similar questions on SO, none of them quite meet my requirements. I am working on a client-side Javascript/HTML5 web application using jQuery Mobile and experiencing slow performance. Suggestions have been made that the issue may b ...

Troubleshooting the issue of empty Axios POST parameters in a MERN stack server

I'm having trouble updating a document in MongoDB using an Axios POST request in my Node.js Express server (MERN stack) - every time I send the params for the updates, the server receives only a blank object. I've attempted to resolve this issue ...

Hiding and Revealing Different Divs using Hide/Show_trait

I have implemented a Javascript code to toggle the visibility of different divs based on icon clicks. However, I am facing an issue where clicking on another icon does not close the previously opened div. I want only one div to be open at a time and for th ...

Utilizing accordion headers with linked images in AngularUI

I came across a helpful post on Stack Overflow that explains how to incorporate header images in an AngularUI accordion. I'm curious if there's a way to use the image as a hyperlink to a website, excluding the text in the header. Appreciate any i ...

Load CKEditor.js with RequireJS following the textarea element

If you need a WYSIWYG editor, CKEditor could be the answer. Check out their documentation here. To properly load CKEditor, make sure to add the following script tag in the header: <script src="../ckeditor/ckeditor.js"></script> ... Then, inc ...

Is it beneficial to use TypeScript for writing unit tests?

We are in the process of transitioning from JavaScript to TypeScript within my team. One question that has come up is whether we should also migrate our unit tests from JavaScript to TypeScript. Personally, I am not convinced of the significant benefits o ...

Is there a way to add an object from one array of objects into a nested array within another array of objects?

I am dealing with two variables in my code: parks and review. let parks = [ { id: 1, park_name: 'Average Park', review: [] }, { id: 2, park_name: 'Better Park', review: [] }, { id: 3, park_name: 'Cooler Park', review: [] ...

Angular 6 - Preload service with asynchronous requests prior to injecting dependencies

I've encountered an issue with my service that loads a configuration object in memory based on the environment file. The problem is that the settings are read asynchronously, causing the application to start before all settings are loaded and resultin ...

How can I utilize the replace function in Angular to swap out a character?

I am attempting to change the characters { and } in an HTML string to {{ and }}. However, when using the replace function, I encounter the following error: $scope.selectedtemplate.template.replace is not a function Below is my code: $scope.selectedte ...

Leveraging findOne and findOneAndUpdate with an HTTP request in mongoose

I am currently developing an API Rest where I need to make HTTP requests using Postman. Specifically, I am trying to search for or update a MongoDB document by an ID that is not the default doc_id provided by Mongo. Models Schema 'use strict' ...

Having trouble with Vue.js? My simple program isn't displaying any output

Just diving into the world of Vue.js, I've been learning it recently. However, this basic code snippet isn't displaying any output. Can someone please provide assistance? <html> <head> <title>Vue Js | Learning Journey </titl ...

What is the process for defining the test browser in jsperf?

While browsing through jsperf, I came across numerous test cases that could run smoothly in various browsers and display results on browserscope. However, to my dismay, when setting up a test, it only seems to work on my own browser version. Any suggesti ...

React application created with create-react-app that uses multiple environment files for varying configurations

I've been working on setting up various environment files for a React project (using create-react-app). I referred to the official documentation, but I encountered the following error: '.env.development' is not recognized as an internal or ...

Instructions for modifying and deleting values within a JSON Array with jQuery

A JSON array is structured as follows: var simple = []; for (var i = 0; i < 5; i++) { simple.push({ id: id, name: name, mobile: mobile }); } The data in the JSON array looks like this: [{id:1 name:"Test" mobile:100}, {id:2 name:"Test ...

How can AngularJS achieve ng-repeat functionality with multiple variables similar to ng-init?

When using ng-init, you have the ability to utilize multiple variables: ng-init="var1=value1; var2=value2" I attempted something similar with ng-repeat but unfortunately it did not work as expected ng-repeat= "var1 in var1s; var2 in var2s" Is there a p ...

CSS Gallery Indicator: Enhancing Your Website's Visual Appeal

After implementing a CSS gallery template into my HTML code, I faced an issue. The original code contained only 4 images, but after adding nine more images, the indicator in the gallery stopped moving past the 4th image and the preview of the additional im ...