user1469734
user1469734

Reputation: 801

Vue2 Search in List received via Axios

since filtering is way more complex then in Vue 1, I have a question.

This is my Component, where a list of Sheeps is shown with the option to search/filter on Name or Family. But I can't figure out how to achieve this.

<input type="search" v-model="search" placeholder="Search for Name OR Family" />
<ul>
    <li v-for="sheep in sheeps"> <!-- Tried also: 'sheep in filteredSheeps' -->
        {{ sheep.name }} ({{ sheep.type }}/{{ sheep.family }} )
    </li>
</ul>
<script>
import axios from 'axios';
export default {
    data() {
            return {
                sheeps: [],
                search: '',
            };
        },
        mounted() {
            this.getSheeps();
        }
        methods: {
            getSheeps() {
                var self = this;
                const url = '/api/sheeps';
                axios.get(url).then(function(response) {
                    self.sheeps = response.data;
                });
            },
        },
        computed: {
            filteredSheeps: function() {
                var self = this;
                return this.sheeps.filter(function(item) {
                    return item.family.toLowerCase().indexOf(self.search.toLowerCase()) > -1
                })
            }
        }
    }
}
</script>

I thought that it needed the computed method filteredSheeps in the v-for, but that's not it either. Getting an error directly then: TypeError: null is not an object (evaluating 'item.family.toLowerCase')"

Upvotes: 0

Views: 2150

Answers (1)

Bert
Bert

Reputation: 82469

Here is a computed that will protect you from cases where the family and/or name is not populated.

filteredSheeps: function() {
  let searchTerm = (this.search || "").toLowerCase()
  return this.sheeps.filter(function(item) {
    let family = (item.family || "").toLowerCase() 
    let name = (item.name || "").toLowerCase()
    return family.indexOf(searchTerm) > -1 || name.indexOf(searchTerm) > -1
  })
}

And a working example.

console.clear()

const sheeps = [
  {name: "Lily", type: "Dorper", family: "Family 1"},
  {name: "Joe", type: "Merino", family: "Family 2"},
  {name: "Bob", type: "Dorper", family: null},
]

new Vue({
  el: "#app",
  data:{
    sheeps:[],
    search: null
  },
  computed: {
    filteredSheeps: function() {
      let searchTerm = (this.search || "").toLowerCase()
      return this.sheeps.filter(function(item) {
        let family = (item.family || "").toLowerCase() 
        let name = (item.name || "").toLowerCase()
        return family.indexOf(searchTerm) > -1 || name.indexOf(searchTerm) > -1
      })
    }
  },
  created(){
    setTimeout(() => this.sheeps = sheeps, 500)
  }
})
<script src="https://unpkg.com/[email protected]"></script>
<div id="app">
<input type="search" v-model="search" placeholder="Search for Name OR Family" />
<ul>
    <li v-for="sheep in filteredSheeps"> 
        {{ sheep.name }} ({{ sheep.type }}/{{ sheep.family }} )
    </li>
</ul>
</div>

Upvotes: 2

Related Questions