You need to manage different states to display relevant messages and use logic to detect them.
- Loading items without any data
- No items available
- Items present without any filtering
- Filtered items with no results
Utilize a computed property to filter your results, which creates a separate copy of filtered items that you can iterate over. This helps in distinguishing the filtered items from the original ones.
For instance, (since there is no StatusComponent it outputs JSON)
<div id="app">
search <input v-model="filter.search"/>
box 1 <input type="radio" v-model="filter.box" :value="1">
box 2 <input type="radio" v-model="filter.box" :value="2">
<div v-if="!loading">
<template v-if="filtered_paints.length">
<div v-for="(paint, index) in filtered_paints" :key="paint.id" class="line">
{{ index }}:
{{ filter.search ? 'searched: ' + filter.search : '' }}
{{ filter.box ? 'in box: ' + filter.box : '' }}
{{ paint }}
</div>
</template>
<template v-else>
<div v-if="!paints.length">
There was an issue loading paints.
</div>
<div v-else>
No matching paints, for:
{{ filter.search ? filter.search : '' }}
{{ filter.box ? 'in box: ' + filter.box : '' }}
</div>
</template>
</div>
<div v-else>
Loading paints...
</div>
</div>
{
data() {
return {
loading: true,
filter: {
search: '',
box: 0
},
paints: [],
};
},
computed: {
filtered_paints() {
return this.paints.filter((i) => {
if (!this.filter.box && this.filter.search === '') {
return true
}
let found = false
if (this.filter.box && this.filter.search) {
if (i.boxid === Number(this.filter.box) && i.name.startsWith(this.filter.search)) {
found = true
}
return found
}
if (this.filter.box && i.boxid === Number(this.filter.box)) {
found = true
}
if (this.filter.search && i.name.startsWith(this.filter.search)) {
found = true
}
return found
})
}
},
mounted() {
console.log('Mock loading some paints');
setTimeout(() => {
this.paints = [
{ id: 1, paintid: 1, boxid: 1, name: 'white' },
{ id: 2, paintid: 2, boxid: 2, name: 'black' }
]
this.loading = false
}, 2000)
}
}
Check out the example online: