[英]Search Box and Checkbox filter with Vue
I am trying to build filter system with Vue. 我正在尝试使用Vue构建过滤系统。
Filters working, but all the functions computed are separeted functions. 过滤器工作,但计算的所有函数都是separeted函数。 So How can I make those in one function and use it. 那么我如何在一个功能中使用它并使用它。
export default { data() { return { estates: [], search: '', regions:['関西','関東','京橋'], checkedRegions:[] } }, created(){ axios.get('/ajax').then((response) => { this.estates = response.data; }); }, computed: { one: function() { var result = this.estates.filter((estate) => estate.building_name.match(this.search) ); if(this.checkedRegions.length && this.checkedRooms.length) { return result.filter(estate => this.checkedRegions.includes(estate.region) && this.checkedRooms.includes(estate.rooms)) } return result; } } }
<div class="container-fluid"> <div class="row"> <div class="col-md-9"> <input type="text" v-model="search" name="" placeholder="search estate" value=""> <div v-for="estate in filteredestate" class="card-body"> <h2>{{estate.building_name}}</h2> <p>{{estate.address}}</p> </div> </div> </div> </div>
filteredestate
filteredRegions
and filteredRooms
make one function. filteredestate
filteredRegions
和filteredRooms
创建一个函数。 for example how to return those function with &&
? 例如如何使用&&
返回这些函数? And use it in this div. 并在这个div中使用它。
<div v-for="estate in oneFunction" class="card-body">
Set your filter search result first as variable and you can check filter by or(||)
expression ! 首先将过滤器搜索结果设置为变量,然后可以通过or(||)
表达式检查过滤器!
I modified
this
inside arrow function by setting to that variable and on the last line returnresult
as default 我通过设置为该变量修改了this
内部箭头函数,并在最后一行返回result
作为默认值
one: function() {
var that = this;
var result = this.estates.filter((estate) =>
estate.building_name == that.search;
);
if(this.checkedRegions.length || this.checkedRooms.length) {
return result.filter(estate => that.checkedRegions.includes(estate.region) || that.checkedRooms.includes(estate.rooms))
}
// when region and room length is 0
return result;
}
}
rooms
and regions
are arrays. rooms
和regions
是阵列。 So you need to iterate through these arrays in order to render the checkboxes. 因此,您需要遍历这些数组才能呈现复选框。
instead of this: 而不是这个:
<input type="checkbox" v-model="checkedLocations" v-bind:value="regions">関東 <input/>
<input type="checkbox" v-model="checkedLocations" v-bind:value="regions">関西 <input/>
<input type="checkbox" v-model="checkedLocations" v-bind:value="regions">北海道<input/>
should be like this: 应该是这样的:
<template v-for="region in regions">
<input type="checkbox" v-model="checkedLocations" v-bind:value="region.id">region.region<input/>
</template>
similar should be done with rooms
. 类似的应该与rooms
。
Also, in js part, you have checkedRegions
while in template you have checkedLocations
. 此外,在JS的一部分,你必须checkedRegions
而在模板中,您有checkedLocations
。 I guess this should be too checkedRegions
. 我想这应该是太过checkedRegions
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.