Vue.jsでの複数タグ検索(フィルタリング)
axiosで受け取ったjsonデータを基に検索し、該当する画像を表示する。
という検索機能を開発しています。
現在は一種類の絞り込みしか実装できていません。
該当のソースコード
vue
1<template> 2 <div class="container"> 3 <div> 4 <input type="text" v-model="keyword" /> 5 </div> 6 7 <div id="scene" class="btn-group" data-toggle="buttons"> 8 <input type="checkbox" v-model="check" value="bedroom" />ベッドルーム 9 <input type="checkbox" v-model="check" value="livingroom" />リビング 10 <input type="checkbox" v-model="check" value="diningroom" />ダイニング 11 <input type="checkbox" v-model="check" value="kitchenroom" />キッチン 12 </div> 13 14 <div class="row"> 15 <a class="col-md-4" 16 :href="'/work-detail?work='+value.post_work_id" 17 v-for="(value,key) in filter" 18 :key="key"> 19 <img v-bind:src="'/storage/'+value.image" /> 20 </a> 21 </div> 22 </div> 23</template> 24 25<script> 26export default { 27 data: function() { 28 return { 29 keyword: "", 30 works: [], 31 images: [], 32 check: [], 33 result: [], 34 old_result: [] 35 }; 36 }, 37 38 computed: { 39 // 検索機能 40 filter: function() { 41 this.result = []; 42 this.old_result = []; 43 for (let x = 0; x < this.works.length; x++) { 44 this.images = this.works[x].post_work_images; 45 for (let i in this.images) { 46 let image = this.images[i]; 47 48 for (let n = 0; n < image.tags.length; n++) { 49 if ( 50 image.tags[n].name.indexOf(this.keyword) !== -1 && 51 image.tags[n].name.indexOf(this.check) !== -1 52 ) { 53 if (this.old_result == image) { 54 } else { 55 this.result.push(image); 56 console.log(image.tags[n].name); 57 } 58 this.old_result = image; 59 } 60 } 61 } 62 } 63 return this.result; 64 } 65 }, 66 67 mounted: function() { 68 axios.get("/api/").then(response => (this.works = response.data)); 69 } 70}; 71</script> 72
あなたの回答
tips
プレビュー