Learn how to retrieve an array within an object by clicking on a specific button

Currently, I am in the process of creating a filter and its corresponding labels. The objective is to allow users to select from various options when they click on the first row. While I have successfully implemented the functionality to display choices for the active button (2nd row), I am facing an issue where it only retrieves the choices for the first button labeled "Stage Performance". It appears that I need to update the ActiveIndex to determine which button's choices should be extracted. How can I modify the activeIndex when the user clicks on the 1st row button?

To get a better visual understanding, take a look at the design: https://i.stack.imgur.com/zrQsu.png

This is the template structure:

<swiper
  :slidesPerView="5"
  :navigation="true"
  :modules="modules"
  class="mySwiper"
>
 // Loop through labels 
  <swiper-slide
    v-for="(label, index) in labels"
    :key="index"
    @click="handleActiveIndex(index)"
  >
    <button class="button-text">{{ label.type }}</button>
  </swiper-slide>
</swiper>
    
// Display sub-labels based on active button selection
<div class="choice-list">
  <div
    class="choice"
    v-for="(choice, index) in labels[activeIndex].choices"
    :key="index"
  >
    {{ choice }}
  </div>
</div>

The script involved looks like this:

setup() {
  /* Filter labels */  
  const activeIndex = ref(0);
  const labels = ref([
    {
      type: "Stage Performance",
      choices: [
        "All",
        "Music",
        "Chinese Opera",
        "Dance",
        "Theatre",
        "Multi-Arts",
        "Pop Concerts",
      ],
    },
    {
      type: "Festivals",
      choices: [
        "All",
        "International Arts Carnival",
        "New Vision Arts Festival",
        "New Vision Arts Festival",
        "Chinese Opera Festival",
        "Hong Kong Pop Culture Festival",
        "Other Festivals / Series Events",
      ],
    },
  // Additional objects... //
  ]);
  return {
    modules: [Pagination, Navigation],
    activeIndex,
    labels,
  };
}
         

Answer №1

Seems like the handleActiveIndex function has not been specified.

const handleActiveIndex = (position) => {
  activeIndex.value = position;
};

return { 
  modules: [Pagination, Navigation],
  activeIndex, 
  labels, 
 handleActiveIndex, // include this function in the return statement 
};

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

ERESOLVE encountered issues resolving the dependency tree for a project using .NET Core 3.1 and Vue.js framework

I encountered an issue while trying to build my application. The error message is as follows: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class ...

What is the syntax for populating an attribute on the same line as v-for in Vue.js?

I am currently working on a simple loop utilizing Vue to iterate over an array of objects and populate table rows. <tr v-for="user in users"> <td>{user.name}</td> <td>{user.id}</td> </tr> However, I also need to as ...

utilizing varying environments for specific scenarios within a nuxt application

I am new to Nuxt or any type of node technology. I am attempting to create different environments for various scenarios. For example, if I want to test my app, I would like the dev object block to run (pointing to a dev endpoint). Here is an example: [ p ...

A Step-by-Step Guide to Setting Up and Utilizing V-Calendar in Vue.js

I am currently trying to incorporate the V-Calendar library into my Vuetify application. Up until now, the app was working fine, but I seem to have hit a roadblock with the correct installation of the V-Calendar library. Although no error messages are bei ...

Injecting background images with CSS in Vue3

After exploring answers to my previous inquiry, I am now seeking guidance on how to inject both a variable and a URL image into my scoped CSS. Despite trying various combinations, none seem to be effective: <template> <header class=< ...

The v-show directive is not activated by a Vuex commit

I am experimenting with vuex for the first time, and I have come across an issue where a v-show directive is not triggering after a mutation commit on the store. // store.js import Vue from "vue" import Vuex from "vuex" const states = ...

Is there a way to turn off linting while utilizing vue-cli serve?

I am currently running my project using vue-cli by executing the following command: vue-cli-service serve --open Is there a way to stop all linting? It seems like it's re-linting every time I save, and it significantly slows down the process of ma ...

vuex issue - do not modify the state of vuex store directly outside of mutation handlers

Can someone assist me with this issue? I am currently working with Quasar 2 (using the Vue 3 framework). When I try to execute state.authorization = data.data.data.authorization; in a mutation, why do I receive the error message "[vuex] do not mutate vue ...

What is the process for integrating tailwindcss into a vite project?

I have recently started using vite version 0.16.6 and I am looking to transition a vuepress website to utilize vite. The main challenge I am facing is figuring out how to set up tailwindcss with vite. In my CSS file named index.css @tailwind base; @tail ...

The dataset remains undefined within the context of Vue.js

I am attempting to utilize the dataset property in Vue to retrieve the data-attribute of an element. Here is how I am implementing it: <ul id="filter"> <li><a class="filter-item" v-on:click="filterItems" data-letter="a" href="#"> ...

Having trouble testing a basic Vue example component in Laravel 5.4

I recently installed Laravel 5.4 and after running npm install, I tested a vue component example but nothing happened as expected. After running npm run dev in my project directory, the compilation was successful. However, when I ran php artisan serve, al ...

Developing modular applications with Vue.js and leveraging locally installed NPM packages

I am currently working on developing a modular application using Vue through the vue-cli-service. The main application and its modules are located in separate folders, with a structure that looks something like this: -- app/package.json /src/** -- mo ...

Controllers in Laravel are not detecting the public function

Recently, I obtained a Laravel/Vue.js project from Bitbucket for the company I am currently collaborating with. Setting it up on my computer seemed like a routine task since I have worked on similar projects in the past. However, this time around, I encoun ...

Creating a Copy of an Object in JavaScript that Automatically Updates When the Original is Changed

I am in the process of developing a planner/calendar website with a repeat feature. var chain = _.chain(state.items).filter({'id': 1}).head().value(); console.log(chain); After filtering one object, I am wondering how to create a duplicate of c ...

Exploring the differences between this.$emit and this.$root.$emit in Vue.js: a guide to best practices

My Vue2 components are dynamically added and removed using v-if. Sometimes, I need to communicate between these components by using $emit in the sender and $on in the receiver. I have been utilizing this.$root.$emit to broadcast custom events and this.$ro ...

Automatic selection of default option

I am currently working on a component that allows users to edit a product. Within the select element, my goal is to set the default option as the product category. I want to automatically select the option that matches the name of the product's catego ...

Troubleshooting issue with Chrome Vue devtools integration in WebStorm not functioning properly

The "open in editor" button in Chrome Vue devtools does not function properly with WebStorm IDE on Macbook Air M1. However, it works perfectly fine with VS Code! ...

Is there a way to identify Vue.js compilation errors proactively, before attempting to serve the application?

Spent hours troubleshooting why my Vue JS app was hanging when running npm run serve. Finally discovered it was due to a single error in one of my Vue files. The issue was hard to identify since it caused the console window to freeze. Are there better way ...

Detecting a component within a slot using Vue

Within my tile component, there is a slot where users can input various html tags, including a specific component called <listitem />. Is there a way to detect if this special component is used within the slot? <div class="tile"> &l ...

Integrating SSL for Nuxt.js 3.9 in a Live Setting

I have a unique project in the works, where I am combining Vue.js/Nuxt.js frontend with Java Spring backend. To streamline deployment, I containerized both components into a single Docker image and then deployed it to a Kubernetes cluster on a different ne ...