0

in my Vue js code i've created a search input with dropdown, the data of drop down are API data array categories i'm trying to create filter on the input search bar so whenever a value typed in input search bar it should show in drop down menu from API, my code here isn't applying the filter (not working).any help?

<template>

  <div class="dropdown">
    <input v-model.trim="inputValue" class="dropdown-input" type="text" placeholder="Find country" />
    <div v-show="inputValue" class="dropdown-list">
      <div   v-for="(category, index) in FilterCategories"
              :key="index" class="dropdown-item"
            {{ category.category_name }}

      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import questionService from "../services/questionService";

export default {
  name: "postComponent",
  components: {},
  
  data() {
    return {
      inputValue: '',
      categories: [],
   
    };
  },
  methods: {
       FilterCategories() { //not working
   
        return  this.categories.filter(categories => {
        return categories.category_name===this.inputValue
        });
     
    },},
    
  mounted: function () {
    questionService.getCategories().then((response) => {
      this.categories = response.data.response;
      
     
    });
  },
};
</script>

3
  • It looks like FilterCategories should be a computed property, not a method Commented Jan 12, 2021 at 16:33
  • Can you try taking out the second return statement? Like return this.categories.filter(categories => { categories.category_name===this.inputValue }); Commented Jan 12, 2021 at 16:36
  • @JIguadeza the second return is the return from the inner filter function; it is correct Commented Jan 12, 2021 at 16:37

1 Answer 1

1

using computed property would be better option

computed: {
       FilterCategories() {
        return  this.inputValue ? this.categories.filter(category => {
          return category.category_name.toLowerCase().includes(this.inputValue.toLowerCase())
        }) : this.categories;  
    }
}

**No changes are required in template

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.