我正在弄清楚我的搜索和过滤功能如何正常工作。我通过创建从搜索结果的搜索功能和过滤器stock
,distance
,price
,和time response
。我的搜索功能运行良好。但是,我制作的过滤器功能仍然无法使用。
我想在搜索后想要进一步过滤搜索,当其中一个下拉菜单更改时,会有诸如股票和距离之类的选项,搜索结果项也会根据过滤器而变化(例如:股票)是否可用。还有其他过滤器,例如价格和时间响应。当我单击价格时,将按照最低价格到最高价格对项目进行排序。当我单击时间响应时,搜索项将按从快速响应到延迟响应的顺序排列。
示例案例
在Type
,我选择BMW
。然后,我单击搜索按钮。将显示2个类型为的项目结果BMW
。然后,在筛选器中,Distance
我选择500 KM
。它应该只显示1个结果。但是过滤器不起作用。
如果有6个项目结果,并且我单击price
该项目,则将按从最低价格到最高价格的顺序进行排序
我已经编写了如下代码,有人可以帮助我解决此问题吗?
new Vue({
el: '#app',
data: {
selectedType: '',
selectedCountry: '',
selectedYear: '',
selectedStock:'',
selectedDistance:'',
items: [{
name: 'Carthy',
type: 'mercedes',
year: '2020',
country: 'england',
stock: 'available',
distance: '500',
price: '1900',
response: 'fast'
},
{
name: 'Holand',
type: 'mercedes',
year: '2020',
country: 'england',
stock: 'available',
distance: '500',
price: '1050',
response: 'fast'
},
{
name: 'Nolan',
type: 'mercedes',
year: '2020',
country: 'england',
stock: 'available',
distance: '500',
price: '1000',
response: 'fast'
},
{
name: 'Edgar',
type: 'bmw',
year: '2020',
country: 'belgium',
stock: 'available',
distance: '5000',
price: '1200',
response: 'fast'
},
{
name: 'John',
type: 'bmw',
year: '2019',
country: 'england',
stock: 'available',
distance: '500',
price: '1500',
response: 'fast'
},
{
name: 'Axel',
type: 'mercedes',
year: '2020',
country: 'england',
stock: 'sold',
distance: '500',
price: '1600',
response: 'late'
}
],
searchResult: [],
itemsToShow: 2,
totalItems: 0,
sortByPrice: true,
sort: 'price',
sortByTime: true,
sort: 'time'
},
computed:{
filterItem: function() {
let filterStock = this.selectedStock,
filterDistance = this.selectedDistance
return this.searchResult.filter(function(item) {
let filtered = true
if (filterStock && filterStock.length > 0) {
filtered = item.stock == filterStock
}
if (filtered) {
if (filterDistance && filterDistance.length > 0) {
filtered = item.distance == filterDistance
}
}
return filtered
})
}
},
methods: {
search: function() {
let filterType = this.selectedType,
filterCountry = this.selectedCountry,
filterYear = this.selectedYear
this.itemsToShow = 2;
this.searchResult = this.items.filter(function(item) {
let filtered = true
if (filterType && filterType.length > 0) {
filtered = item.type == filterType
}
if (filtered) {
if (filterCountry && filterCountry.length > 0) {
filtered = item.country == filterCountry
}
}
if (filtered) {
if (filterYear && filterYear.length > 0) {
filtered = item.year == filterYear
}
}
return filtered
})
},
priceSort: function(){
this.sortByPrice = !this.sortByPrice
if(this.sortByPrice)
this.sort = 'price'
},
timeSort: function(){
this.sortByTime = !this.sortByTime
if(this.sortByTime)
this.sort = 'time'
}
},
mounted() {
this.search()
}
})
.list-item{
margin-top:50px;
}
#app{
position:relative;
padding-bottom: 200px;
}
span{
margin: 0 15px;
cursor:pointer;
}
.filter-box{
margin-top:15px;
}
.card{
box-shadow:0px 10px 16px rgba(0,0,0,0.16);
width:400px;
padding:20px 30px;
margin-bottom:30px;
}
button{
background-color: #1cf478;
border:none;
padding: 10px 25px;
font-weight:bold;
border-radius: 15px;
}
select{
border:none;
padding: 10px 15px;
background-color:#c1c1c1;
border-radius:10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.1/vue.js"></script>
<div id="app">
<div class="search-box">
<select v-model="selectedType">
<option value="" disabled selected hidden>Type</option>
<option value="mercedes">Mercedes</option>
<option value="bmw">BMW</option>
</select>
<select v-model="selectedCountry">
<option value="" disabled selected hidden>Country</option>
<option value="belgium">Belgium</option>
<option value="england">England</option>
</select>
<select v-model="selectedYear">
<option value="" disabled selected hidden>Year</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
<button @click="search">Search</button>
</div>
<div class="filter-box">
<h6>Filter:</h6>
<select v-model="selectedStock" @change="filterItem">
<option value="" disabled selected hidden>Stock</option>
<option value="sold">Sold</option>
<option value="available">Available</option>
</select>
<select v-model="selectedDistance" @change="filterItem">
<option value="" disabled selected hidden>Kilometers</option>
<option value="500">500 KM</option>
<option value="5000">5000 KM</option>
<option value="10000">10.000 KM</option>
</select>
<span class="price" @click="priceSort">Price</span>
<span class="response" @click="timeSort">Time Response</span>
</div>
<section class="result">
<div class="container-fluid">
<div class="row list-item" v-for="(item, id) in searchResult" :key="id">
<div class="col-3 card" v-if="id < itemsToShow">
<p>Name: {{ item.name }}</p>
<p>Car: {{ item.type }}</p>
<p>Year: {{ item.year }}</p>
<p>Country: {{ item.country }}</p>
<p>Price: ${{ item.price }}</p>
<p>stock: {{ item.stock }}</p>
<p>distance: {{ item.distance }}</p>
</div>
</div>
<div class="row">
<div class="col-12">
<button @click="itemsToShow += 1">Load More</button>
</div>
</div>
</div>
</section>
</div>
请参考此codeandbox以根据您的代码进行演示。
filterResult
在data
之上添加searchResult
来处理过滤器结果,我们仅将其呈现filterResult
为结果。这将把“搜索”和“过滤器”的逻辑分开,使逻辑更清晰。filterItems
方法作为@click
处理程序。(以前您使用的是计算属性,这还不够简单,我们可以简单地调用方法@change
并更新filter results
数据。filterResult
与searchResult
没有过滤器值的情况相同)。span
元素,我们只能更新this.sort
数据,并且只要排序标准发生变化,就可以使用watch
触发sortItems
方法来简化方法。您可能需要sortItems
根据需要调整方法。当然,您可以为这两个“排序”按钮编写两个单独的方法,完全由您决定:)本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句