Utilizing Vue.js to add functionality for navigation buttons allowing users to move between survey questions

In my Vue.js component, I've written code to show survey questions in a mobile app for users. Here is a snippet of the code:

      <div class="col-12 p-0" v-for="( i, index ) in questions" :key="i">
        <p class="lead fw-bold">{{ item.question }}</p>
        <div class="form-check" v-for="(choice, index) in item.choices" :key="index">
          <input class="form-check-input" type="radio" :name="i" :value="index" @change="checkAnswer(item.questionIndex, index)" :disabled="answeredQuestions[i]">
          <label class="form-check-label" for="">{{ index }}) {{ choice }}</label>
        </div>
      </div>
export default {
  name: 'Quiz',
  data() {
    return {
      answeredQuestions: []
    }
  },
  mounted() {
    front.on('questions', (data) => { 
      console.log(data) 
      this.$store.commit('quizQuestions', data);
      this.$store.commit('contentLoaded', true);
    });
  },
  computed: {
    isLoading() {
      return this.$store.getters.showLoader;
    },
    questions() {
      return this.$store.getters.quiz;
    },
  },
  methods: {
    checkAnswer(questionIndex, choice) {
      this.answeredQuestions.push(true);
      ...
    }
  }
}

To avoid scrolling, I want to add a button that displays the next question when clicked. However, I'm not sure how to do this with v-for and v-if. How should I proceed?

Answer №1

If you're looking to add buttons to your Swiper, you can do so by using the Swiper plugin found here. Alternatively, if you want to link each question to a specific route, you can achieve this by updating the current route when each question is selected - simply pass the handler function to your buttons using

$route.push('surveyId/questionId')
calls. Implementing either option will eliminate the need for using v-if with v-for in your code.

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

Exploring the use of functions in the setup method of Vue 3

I'm currently working on a simple app and utilizing mock-json-server to mimic http requests. Within my project, I have defined a function designed to retrieve the necessary information: import { ref } from 'vue' const getScores = () => ...

Encountering ENOENT error with Node.js file preview

I am attempting to utilize the filepreview tool to extract an image from a docx document. I have successfully installed it using the command npm install filepreview. This is the code snippet I am working with: const filepreview = require('fileprevie ...

What is the best way to incorporate the {id} property into my Next.js dynamic route using the Context API?

I am encountering an issue with passing the ${id} property to my dynamic route [id]>page.jsx, located within the CartItemPage.jsx file. The setup involves using the Context API, similar to React, where I maintain an array of CartItems to render the Cart ...

When attempting to select dates from the picker options, the array is found to be devoid of any entries

My challenge lies in working with an array of dates retrieved from the server to determine which dates should be disabled on the datepicker. getStaffAvailability(){ let x = this; this.$http.get(this.weeklyAvailabilityUrl + "GetAv ...

New Angular Datatables used to create a revitalizing table

In my project, I am utilizing the Angular Datatables library. The data is fetched from a URL that returns a JSON object, which is then stored in an array and used to populate a table. appenditems(){ this.items = []; this.items2 = []; this.items ...

NodeJS - Issue: The procedure specified could not be located in the bcrypt_lib.node

After upgrading from Node.js 0.12.7 to 4.2.1, I encountered an error when attempting to start my server: $ node server.js C:\Users\me\documents\github\angular-express-auth\node_modules\bcrypt\node_modules\bindi ...

Dynamic class/interface in Typescript (using Angular)

Is there a way to achieve intellisense for an object created with a dynamic class by passing parameters? Here is the code snippet: Main: let ita: any = new DynamicClass('ITA'); let deu: any = new DynamicClass('DEU'); The DynamicClass ...

Express/axios fails to properly configure cookies and headers for response delivery

I am facing a complex issue with my project that involves two APIs. One API serves as the front-end of the application, while the other is for the back-end. The process simply entails sending requests from the front-end API to the back-end API. The front- ...

Will "Access-Control-Allow-Origin" provide protection if someone directs their domain to my server?

It seems I may have misinterpreted the full implementation of CORS on my server. Looking at this screenshot of a request made through Chrome. We can observe that we are accessing the site shakh.photography, where the request URL is a POST ajax request to ...

Remove the 'name' attribute from the input tag within the URL

Is there a way to make the server update the URL format to localhost:3000/userinput instead of localhost:3000/?name=userinput when submitting a name? Any suggestions would be greatly appreciated. Thank you in advance. ExpressJs FILE <!DOCTYPE html> ...

html form submission error - please refresh

After submitting a form on my page, I keep encountering an error every time I try to refresh the page. I've attempted several solutions that I came across online, but none of them have resolved the issue as I continue to face this error on different ...

Updating a form field dynamically with AJAX in Laravel

I'm working on updating the field $medic->current based on certain logic that I have in mind. I'm quite new to using AJAX and would appreciate a little guidance to kick things off. In my code, I am calculating the difference between two dates ...

What causes json.parse to malfunction? and how can you resolve the issue

My server sends data to my JavaScript code in the format below. {"triggers": [{"message_type": "sms","recipients": "[\"+91xxxxxxxxx\",\"+91xxxxxxxxx\"]", "message": "This is a test"}]} To parse this JSON string, my code executes the f ...

Utilize D3 to Rotate Text within an SVG in Circular Motion, Following by Self-rotation

In my visualization, the labels are arranged around the center of a circle by rotation. However, this results in the labels on the left side of the circle appearing upside down. Is there a way to rotate the labels on the left side independently after the i ...

Check if the content key Json exists by implementing Vue

Can anyone help me with checking the existence of "novalue"? For instance: { name: "maria", city_id: "novalue" .... } What would be the best way to do this in Vue? Should I use <div v-if="condition"> or a function? ...

I'm looking for a way to set up the dev server using a basic vue.js/webpack configuration to proxy everything except specific .js and .vue files. How can this be achieved

Let me give you a brief overview of our current website setup: Our company's website is currently powered by a CMS. All pages are dynamically generated and routed through the CMS, so we don't have any .html files in our system. The content is ge ...

Delete the item along with its associated data from the local storage upon clicking on an icon

I'm currently working on a To-Do List and facing an issue while trying to delete an item upon clicking a trash bin icon. image The problem I'm encountering is that only one data point is removed from local storage when the icon is clicked. If I ...

Empty placeholder image appearing at the beginning of JavaScript slideshow in Ruby on Rails

As a newcomer, I ask for your understanding if I lack knowledge in some areas. I have been working on a project and using the cycle2 plugin successfully to create a slideshow on my index page with images pulled from the internet. Now, I am trying to impl ...

Looking to incorporate the Vue3Mq plugin into a Quasar application - What is the best way to set it up?

I've been attempting to set up a Quasar JavaScript application to utilize a Vue plugin (Vue3Mq) for media query functionality. Despite installing Vue3Mq via npm, my efforts have not yielded the desired results. Here's what I've tried so far ...

When attempting to toggle the view on button click, it is not possible to select a shadowRoot

I am facing an issue with my parent component named ha-config-user-picker.js and its child component called edit-user-view.js. Parent Component: It contains a mapping of users and includes the child component tag along with its props. When a click event i ...