Basically, this code is responsible to display into a dropdown the list of cars name contained inside the sortedCars array. (This array is sorted by string asc).
Now, I added an input text, and I'm trying to filter automatically the list of cars matching with the entered name value.
With the following code, the UI is never updated. Where is the mistake ?
<template>
<div class="btn-group pull-left">
<input type="text" @keyup="onFilterCars" v-model="searchCarTextValue" />
<a v-for="car in this.sortedCars" :key="car.name" @click="onSelectCategoryFilterLabel" class="dropdown-item" href="#">
<span v-else>{{ car.name }}</span>
</a>
</div>
</template>
<script>
export default {
data() {
return {
inputFilterText: "",
mapFilteringEnabled: false,
searchCarTextValue: ""
};
},
props: {
cars: {
type: Array
}
},
computed: {
sortedCars: function() {
function compare(a, b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
}
var sortedArr = this.cars.sort(compare);
return sortedArr;
}
},
methods: {
onFilterCars() {
if (this.searchCarTextValue) {
var arrayFiltered = this.sortedCars.filter( (car) => {
return car.name.toLowerCase().indexOf(this.searchCarTextValue.toLowerCase()) != -1
});
this.sortedCars = arrayFiltered;
}
}
}
};
</script>
this.sortedCarschanges every time, so after a while it contains nothing, instead you should save the list of all cars somewhere else and filter from that list without altering it, so effecticvely have two lists one with all cars and one filteredcomputedproperty will automatically recompute when its dependent properties change. You dont need theonFilterCarshandler at all.div"twice....not the answer but a problemspan v-elsewithout anv-if