I need help combining the text search and sorting functions. #11063
Unanswered
danbraun
asked this question in
Help/Questions
Replies: 1 comment 1 reply
-
Generally, you can reference one computed property inside of another via However, you computed property for sorting doesn't make sense, really. All it does it return an object with 3 functions. It doesn't compute anything, that would only happen when you call these functions later, which defeats the purpose of a computed property. You could just as well just put these 3 functions into But what you should instead do is this: <template>
<ol>
<li
v-for="{ name, image, id } in orderList"
:key="id"
>
</ol>
</template>
<script>
import { defineComponent } from 'vue';
export default defineComponent({
data: () => ({
// ...
}),
computed: {
filteredList() {
return this.Birds.filter(bird => {
return bird.name.toLowerCase().includes(this.search.toLowerCase())
})
},
orderedList() {
switch(this.sortOrder) {
'Popularity': return this.filteredList;
'A-Z': return this.filteredList.slice().sort(this.sortTitle);
'Z-A': return this.filteeredList.slice().sort(this.sortTitle).reverse();
}
}
});
</script> You could also switch it around and first sort, then filter the sorted data, if filtering happens more often than sorting. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! Just starting into my journey with vue.
I have added 2 computed functions that work separately, but I want to them to work together! I took these both from Codepens, but I understand them.
I have in my component a few sort buttons. They work by changing a data value "sortOrder" which triggers a re-looping through the results of the computed function:
Here's the other thing I found which works great for a text search, if I just swap out my loop to use this function instead:
These work both great, but where do I start to combine them, so that I can have the user both text search and sort on the same "Birds" list?
Beta Was this translation helpful? Give feedback.
All reactions