3 回答

TA貢獻1936條經(jīng)驗 獲得超7個贊
這能解決您的問題嗎?
possibleFilters() { return [...new Set(this.products.map(x => x.location))] },

TA貢獻1786條經(jīng)驗 獲得超11個贊
這是一個基于您的小提琴的快速而骯臟的解決方案。只是為了讓您了解如何分離兩個過濾器。嘗試這里的解決方案https://jsfiddle.net/4839spkx/
possibleFilters() {
// we have some input, so show all location filters available in the filtered by input products
if (this.search) {
const filteredByInput = this.products.filter(p => p.name.includes(this.search.toLowerCase()))
return [...new Set(filteredByInput.map(p => p.location))]
}
return [...new Set(this.filteredSearch.map(x => x.location))]
},

TA貢獻1818條經(jīng)驗 獲得超3個贊
由于您使用計算屬性從過濾的產(chǎn)品位置動態(tài)生成可能的過濾器,因此每次更新過濾的產(chǎn)品時都會更新。我建議您創(chuàng)建一個新數(shù)組并用您的數(shù)據(jù)填充該數(shù)組。
HTML:
...
<li v-for="filter in possibleFiltersArr" :key="filter">
<label>
<input type="checkbox" @change="toggleFilter(filter)" :checked="filters.includes(filter)">
<span>{{filter}}</span>
</label>
</li>
...
JS
...
data: {
posibleFiltersArr:[],
...
created(){
this.possibleFiltersArr=[...new Set(this.filteredSearch.map(x => x.location))]
},
...
您可以在方法中設置此數(shù)組created(),并且可以在搜索框中輸入一些文本后更新此數(shù)組。
- 3 回答
- 0 關注
- 215 瀏覽
添加回答
舉報