2👍
✅
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/vue@2.4.2"></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>
👤Bert
Source:stackexchange.com