Clearing a Vue.js filter: a step-by-step guide

Below is my Vue Js code where I have successfully implemented a filter to API array elements. However, I am facing an issue when trying to set up a button that clears the filter upon clicking, restoring the page to its original state without any changes. Unfortunately, my current code below doesn't seem to work as it displays an empty array of questions. Any assistance would be greatly appreciated. Thank you in advance!

<template>

<div class="container" width=800px>

  <b-row>
  <b-col cols="8">
  <h1> Recently Asked </h1>

     <ul class="container-question" v-for="(question1,index) in questions" :key="index"  
  >
    
   <li>
     {{question1.question}}

     </li></ul>

  </b-col>
<b-button class="outline-primaryy" style="margin:auto;" @click="ClearFilter" :disabled="selectedIndex === index ">Clear Filter</b-button>
        </div>

<router-view />

 </div>

   
</template>
<script>
 
  
export default {

    data(){
    return{
      questions: [],
       answered: null,
      index: 0,
     selectedIndex: null,
     
     
    }
  },
methods: {

 selectedAnswer(index) {
      this.selectedIndex = index;
      this.questions=this.questions.filter((question) => question.incorrect_answers.includes(index))
      console.log(index)
   
    },
    
    ClearFilter(){
    this.questions = []
    },
  

 watch: {
    question1: {
      handler() {
        this.selectedIndex = null;
        this.answered = false;
       
      },
    },
  },

},

 



  mounted: function(){
fetch('https://opentdb.com/api.php?amount=10&category=9&difficulty=medium&type=multiple',{
  method: 'get'
})
.then((response) => {
  return response.json()
})
.then((jsonData) => {
  this.questions = jsonData.results
})
  },
  

}


    

</script>

Answer №1

It's possible that I may have misunderstood your question. However, here is a potential solution:

.then((jsonData) => {
  this.questions = jsonData.results;
  this.unmutated = jsonData.results;
})

When you click the reset button:

this.questions = this.unmutated 

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

Using the data from two input fields, an ajax request is triggered to dynamically populate the jQuery autocomplete feature

I've been struggling with this issue for quite some time now. My goal is to pass 2 arguments (the values of 2 input fields in a form) in my ajax call so I can use them for a jQuery autocomplete feature (the search is based on a MySQL query using the v ...

Clicking on an iframe activates the loading of the displayed page

I'm attempting to create a functionality where clicking on an iframe will load the page it is displaying. I experimented with placing it within an tag, but that didn't produce the desired result. The effect I'm aiming for is similar to zoom ...

Nuxt: Configure Axios requests to have their origin set based on the current domain

Currently, I am utilizing @nuxtjs/proxy for making proxy requests. The setup in nuxt.config.js is working perfectly fine. nuxt.config.js proxy: { '/api/': { target: 'api.example.com', headers: { 'origin': &apo ...

Generate div elements dynamically for each object being populated in JSON using JavaScript

I have a main container that displays a list of JSON objects. Each object should be displayed as a separate DIV element with specific details, one after the other. Additionally, I have a comments section on a webpage where the details are stored in JSON f ...

The display of the selected input is not appearing when the map function is utilized

I am attempting to use Material UI Select, but it is not functioning as expected. When I use the map function, the default value is not displayed as I would like it to be. However, it does work properly when using the traditional method. *** The Method th ...

Is there a way to output several lines from a JSON file in print form?

I'm working with HTML code that displays multiple lines from a JSON file, but it only shows one line at a time. How can I modify the code to display all users' information? <!DOCTYPE html> <html> <head> <script> function ...

Unable to transfer data successfully from popup to extension.js

I am currently developing a browser extension using Crossrider and I'm facing an issue with sending data from the popup to extension.js Here is my code for the popup: <!DOCTYPE html> <html> <head> <!-- This meta tag is relevant ...

Reactjs encountering issues loading css file

Currently, I am working on a project in Reactjs with Nextjs. To add CSS to my project, I have stored my CSS files in the 'styles' folder. In order to include them, I created a file called '_document.js' and implemented the following cod ...

Using VueJS to dynamically insert API data into a text field

After retrieving text from an API, I need to customize it by injecting or substituting variables before displaying it. To better understand what I mean, please check out the example below: https://jsfiddle.net/0q4ot5sw/ new Vu ...

Utilize the power of jQuery to make an ajax request to a PHP backend, expecting a response in JSON format. Then,

Having some trouble with my jQuery code when trying to parse a JSON object returned by PHP and create a list of hyperlinks. Despite receiving the JSON object, my list turns out empty. Can anyone assist me? Below is the jQuery code snippet and the JSON resp ...

What's stopping me from installing the npm stripe checkout package?

After running the command npm install vue-stripe-checkout, I encountered the following error: vue.runtime.esm.js?2b0e:5106 Uncaught TypeError: Cannot read property 'install' of undefined at Function.Vue.use (vue.runtime.esm.js?2b0e:5106) at eval ...

Issue with pop-up functionality on web page using HTML, CSS, and JavaScript

Recently, I created a unique popup using HTML. You can see the complete code (excluding CSS) here: https://codepen.io/nope99675/pen/BawrdBX. Below is the snippet of the HTML: <!DOCTYPE html> <html> <head> <meta charset=&quo ...

Create a debounced and chunked asynchronous queue system that utilizes streams

As someone who is new to the concept of reactive programming, I find myself wondering if there exists a more elegant approach for creating a debounced chunked async queue. But what exactly is a debounced chunked async queue? While the name might need some ...

What is the best way to combine a string that is constructed across two separate callback functions using Mongoose in Node.js?

I am facing a situation where I have two interconnected models. When deleting a mongo document from the first model, I also need to delete its parent document. There is a possibility of an exception being thrown during the second deletion process. Regardl ...

Utilize Moment.js in AngularJS for formatting dates

I have been attempting to use moment.js in Angularjs to format a date, but it seems like I am running into some issues. Here is the link to my code snippet on jsfiddle http://jsfiddle.net/sed6x5e8/ and below you can find the HTML and JS code that I am work ...

How can I get rid of the excessive white space in my Vue.js/Vuetify dialog box?

I am facing an issue with my web app's dialog box where there is empty white space appearing between the v-cards and the v-card-action buttons (Save and Cancel). Is there a way to remove this whitespace or scrollbar? I attempted to place the Cancel/S ...

The functionality of Vue is acting up with the HTML, unexpectedly refreshing when a button is clicked

I am currently experiencing an issue with my Vue application. When I click the button, it clears the input field (which it shouldn't) and doesn't perform any other actions. The variables "codigo" and "payload" do not display anything on the scree ...

What is the most effective way to refine the ${{ data }} object to display only particular values?

Utilizing vue.js, I am retrieving data ${{ data }} and presenting it to the user. However, I only want to showcase specific values. In this case, I wish to display everything except for Actions. The information to be displayed includes: Name, Description, ...

Execute the knockout function using jQuery

There's a scenario where I am trying to trigger a knockout method using jQuery. The Knockout viewModel has already been bound, but I'm unsure of how to call it using jQuery. Below is the snippet of my code: $(document).ready() { form_submit( ...

Inserting a line break in real-time within a JSX statement

Currently working on a React application that retrieves song lyrics from an API. The API provides me with a lyrics_body attribute as a string, which I use to showcase the lyrics on the webpage. However, when React renders it, the format is not ideal becau ...