Created
August 12, 2019 06:14
-
-
Save batbaatap/538c1c6c8d9a9fba5a0ef27d38d80b1d to your computer and use it in GitHub Desktop.
Vue array multi-filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="app"> | |
<template v-for="tag in tags"> | |
<input type="checkbox" value="{{ tag }}" v-model="selectedTags" id="tag-{{ tag }}"> | |
<label for="tag-{{ tag }}">{{ tag }}</label> | |
</template> | |
<p v-show="selectedTags.length != 0"> | |
Filters: <span v-for="tag in selectedTags" class="label">{{ tag }}</span> | |
</p> | |
<ul> | |
<li v-for="card in activeCards">{{ card.name }}</li> | |
</ul> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = new Vue({ | |
el: '#app', | |
data: { | |
tags: ['php', 'js'], | |
cards: [ | |
{ | |
tags: ['php'], | |
name: 'PHP Project' | |
}, | |
{ | |
tags: ['php', 'js'], | |
name: 'Laravel Project php' | |
}, | |
{ | |
tags: ['js'], | |
name: 'Node Project' | |
} | |
], | |
selectedTags: [] | |
}, | |
computed: { | |
activeCards: function() { | |
if(this.selectedTags.length == 0) return this.cards; | |
var activeCards = []; | |
var filters = this.selectedTags; | |
this.cards.forEach(function(card) { | |
function cardContainsFilter(filter) { | |
return card.tags.indexOf(filter) != -1; | |
} | |
if(filters.every(cardContainsFilter)) { | |
activeCards.push(card); | |
} | |
}); | |
return activeCards; | |
} | |
} | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.21/vue.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://cdn.jsdelivr.net/foundation/6.2.0/foundation.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment