1

Creating a filter to sort through quotes data, but having problems with creating the correct functions.

HTML:

<template>
 <div class="home">
 <h1>{{ message }}</h1>
 <h3> Theme filter: </h3>
 <div>
 <button v-on:click="userFilterKey = 'movies'"> Search movies</button>
 <button v-on:click="userFilterKey= 'all'"> Search all</button>
 <button v-on:click="userFilterKey= 'games'"> Search games </button>
 <div v-for="quote in quotes" v-bind:key="quote.id">
 <p>Souce: {{ quote.source }} </p>
 <p>Quote: {{ quote.quote }} </p>
 <p>Theme: {{ quote.theme }} </p>
 </div>
 </div>
 </div>
</template>

Script:

import axios from "axios";
export default {
 data: function () {
 return {
 message: "Welcome to Vue.js!",
 quotes: [],
 userFilterKey: "all",
 };
 },
 methods: {
 userFilter: function () {
 return this[this.userFilterKey];
 },
 all: function () {
 return this.quotes;
 },
 movies: function () {
 return this.quotes.filter((theme) => (quotes.theme = "movies"));
 },
 books: function () {
 return this.quotes.filter((theme) => (quotes.theme = "books"));
 },
 quotesIndex: function () {
 axios
 .get("linktodata")
 .then((response) => {
 this.quotes = response.data;
 });
 },

How do I create filters to sort through the theme key of the quotes array within my link?

asked Jul 19, 2021 at 20:26
1
  • could you make a codepen with example data (not axios). Commented Jul 19, 2021 at 20:51

2 Answers 2

1

Use a computed prop.

new Vue({
 el: '#app',
 data: () => ({
 quotes: [{
 id: 1,
 source: 'a',
 quote: '',
 theme: ''
 },
 {
 id: 2,
 source: 'b',
 quote: '',
 theme: 'movies'
 },
 {
 id: 3,
 source: 'c',
 quote: '',
 theme: 'games'
 }
 ],
 userFilterKey: "all"
 }),
 computed: {
 filteredQuotes() {
 if (this.userFilterKey === 'all') {
 return this.quotes
 }
 return this.quotes.filter(v => v.theme === this.userFilterKey)
 }
 }
})
<div id="app">
 <button @click="userFilterKey = 'all'"> Search all </button>
 <button @click="userFilterKey = 'movies'"> Search movies</button>
 <button @click="userFilterKey = 'games'"> Search games </button>
 <div v-for="quote in filteredQuotes" :key="quote.id">
 <p>Souce: {{ quote.source }} </p>
 <p>Quote: {{ quote.quote }} </p>
 <p>Theme: {{ quote.theme }} </p>
 </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.14/vue.min.js"></script>

tony19
140k23 gold badges281 silver badges354 bronze badges
answered Jul 19, 2021 at 20:54
Sign up to request clarification or add additional context in comments.

Comments

0

Try using Pipes. Its something that does the filter in client side and is faster. may be that solves your problem

answered Jul 19, 2021 at 20:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.