Am creating a search filter using vue js, my current problem is, i want to display datas from recentSearch history when the page created or instead of showing all json data from recentSearch, then when i start typing it will retrieve filter data from searchProduct
My json format in searchProduct.php
{"result" : [
{ product_name: 'mike', business_name: 'student' },
{ product_name: 'beckham john', business_name: 'footballer' },
{ product_name: 'walcott', business_name: 'footballer' },
{ product_name: 'cech', business_name: 'footballer' },
{ product_name: 'jordan', business_name: 'actor' },
{ product_name: 'tom', business_name: 'actor' },
{ product_name: 'john', business_name: 'actor' }
],
"recent" : [
{ product_name: 'mike', business_name: 'student' },
{ product_name: 'beckham john', business_name: 'footballer' },
{ product_name: 'walcott', business_name: 'footballer' }
]}
Html example
<div id="SearchVueContenPage">
<input type="search" v-model="q" name="q"/>
<ul>
<li v-for="row in filterSearchs" >{{row.product_name}}</li>
</ul>
</div>
Here is my javascript
const appSearch = new Vue({
el: "#SearchVueContenPage",
data() {
return {
searchProduct: [], /*show this item only for search*/
recentSearch: [], /*show this when page load or not searching*/
q: ''
}
},
methods: {
},
created() {
fetch(ajax_appserver("public", "_api/searchProduct.php?fetch_api=true&getFromApp=vue&search="))
.then(response => response.json())
.then(json => {
this.searchProduct = json.result;
this.recentSearch = json.recent.slice(0, 3);
});
},
computed: {
filterSearchs: function(){
return this.searchProduct.filter((row) => {
var query = this.q.toLowerCase();
return row.product_name.toLowerCase().indexOf(query)>-1 || row.business_name.toLowerCase().indexOf(query)>-1;
});
}
}
});