简体   繁体   English

如何过滤两个类别的搜索输入?

[英]How to filter search input for two categories?

Following filter function computes the input of a search input field to only display elements, with a similar title like the search input :以下过滤器 function 计算search input field的输入以仅显示元素,其titlesearch input类似:

const filteredNews = computed(() => {
    if (state.filter) {
        return props.news.filter(item => {
            return state.filter.toLowerCase().split(" ").every(v => item.title.toLowerCase().includes(v))
        });
    } else {
        return props.news;
    }
})

search input field : search input field

<input class="filter-input" type="search" placeholder="Suche" v-model="state.filter">

the elements are then displayed in a v-for loop:然后将元素显示在v-for循环中:

<div class="news-gallery" v-for="(card, key) in filteredNews" :key=key>
    // items...
</div>

Now I want to filter not only for title but also location .现在我不仅要过滤title还要过滤location How would I need to change the filter function to achieve that?我需要如何更改filter function来实现这一点?

Something like:就像是:

const filteredNews = computed(() => {
    if (state.filter) {
        return props.news.filter(item => {
            return state.filter.toLowerCase().split(" ").every(v => {
                item.title.toLowerCase().includes(v),
                item.location.toLowerCase().includes(v)
            })
        });
    } else {
        return props.news;
    }
})

I found a very simple way.我找到了一个非常简单的方法。 Just change:只是改变:

item.title.toLowerCase().includes(v)

to:至:

item.title.toLowerCase().includes(v) || item.location.toLowerCase().includes(v))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM