I am trying to filter a v-for loop list of stores. The Vue code I have works if filtering by "store", but I'm struggling to find out how to have the search filter work with nested json like "section_name" and even deeper "items".
examples:
search "milk" -> Store A dairy section only (match daily/almond milk in item)
search "fruit" -> Store B fruit section only (match fruit sectio_name)
search "green" -> Store A veggie section only (match green beans item)
<script setup>
import { computed, reactive, ref } from "vue";
const stores = reactive([]);
const mySearch = ref("");
fetch("http://localhost:8081/sample.json")
.then((res) => res.json())
.then((data) => {
data.forEach(el => {
stores.push(el)
});
})
const filterStores = computed(() => {
return stores.filter((store) => {
return store.store.toLowerCase().indexOf(mySearch.value.toLowerCase()) != -1
});
})
</script>
<template>
<main>
<div class="container-fluid">
<div class="row header">
<div class="d-flex justify-content-center mt-0 p-4">
<input type="text" placeholder="select store" class="form-control form-control-lg" v-model="mySearch"/>
</div>
</div>
<div class="row">
<div class="container">
<div v-for="(store, index) in filterStores" :key="index">
<li>{{ store.store }}</li>
</div>
</div>
</div>
</div>
</main>
</template>
sample.json file
[
{
"store": "Store A",
"data": [
{
"section_name": "veggies",
"section_num": "301",
"items": [
"carrots",
"green beans"
]
},
{
"section_name": "dairy",
"section_num": "302",
"items": [
"dairy milk",
"almond milk"
]
}
]
},
{
"store": "Store B",
"data": [
{
"section_name": "fruit",
"section_num": "336",
"items": [
"apples",
"oranges"
]
},
{
"section_name": "bread",
"items": [
"wheat bread"
]
}
]
}
]
applestill gets theapplesto match. So it depends how 'good' the search ability needs to be but most humans will probably appreciate search predicatbility they are more used to. When I was learning to use elastic with a node app this article got me there really quickly markpenovich.com/posts/…