我正在尝试过滤给定数组“ db”的结果,有三个过滤器: 价格,期限和类别。
我尝试使用filter()方法过滤结果。
这是我尝试过的代码链接:https://stackblitz.com/edit/multiple-filters?file=app%2Fapp.component.ts
<div class="filters">
<div id="price">
<h3>Price: {{rangeValues[0] + ' - ' + rangeValues[1]}}</h3>
<p-slider [(ngModel)]="rangeValues" (click)="handleChange()" [min]="0"
[max]="2000" [style]="{'width':'14em'}" [range]="true"></p-slider>
</div>
<hr>
<div id="duration">
<h3>Duration</h3>
<li><input type="checkbox" (click)="checkFilter('2 days')"> 2 Days</li>
<li><input type="checkbox" (click)="checkFilter('6 days')">6 Days</li>
</div>
<hr>
<div id="Theme">
<h3>Theme</h3>
<li><input type="checkbox" id="Wild Life" (click)="filterTheme('Wild Life')">Wild Life</li>
<li><input type="checkbox" id="Romance" (click)="filterTheme('Romance')">Romance</li>
<li><input type="checkbox" id="Food & Drink" (click)="filterTheme('Food & Drink')">Food & Drink</li>
<li><input type="checkbox" id="Adventure" (click)="filterTheme('Adventure')">Adventure</li>
</div>
<hr>
</div>
<div class="results">
<h3>Results</h3>
<div *ngFor="let p of package">
<p>{{p.product_name}}</p>
</div>
</div>
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
db=[
{
"id":"1",
"product_name":"6 Days at Karnataka",
"price":324,
"duration":"6 days",
"category":["Romance"]
},
{
"id":"5",
"product_name":"2 Days at Thailand",
"price":234,
"duration":"2 days",
"category":["Romance","Wild Life"]
},
{
"id":"8",
"product_name":"2 Days at Delhi",
"price":1400,
"duration":"2 days",
"category": ["Romance","Food & Drink","Adventure"],
}
];
rangeValues: number[] = [0,2000];
package:any;
filterData=[];
ngOnInit(){
this.packageList();
}
packageList(){
for(let i = 0; i < this.db.length; i++){
this.filterData.push(this.db[i]);
this.package =this.filterData;
}
}
handleChange() {
for(let i = 0; i < this.filterData.length; i++){
this.package= this.filterData.filter(item => item.price >=this.rangeValues[0] && item.price <=this.rangeValues[1]);
}
}
checkFilter(id){
if(id.checked==true){
for(let i = 0; i < this.filterData.length; i++){
this.filterData= this.filterData.filter(item => item.duration !== id && item.price >=this.rangeValues[0] && item.price <=this.rangeValues[1]);
this.package=this.filterData;
}
}
else {
for(let i = 0; i < this.filterData.length; i++){
this.filterData= this.filterData.filter(item => item.duration == id && item.price >=this.rangeValues[0] && item.price <=this.rangeValues[1]);
this.package=this.filterData;
}
}
}
filterTheme(id){
let b=(<HTMLInputElement>document.getElementById(id));
if(b.checked==true){
for(let i = 0; i < this.filterData.length; i++){
for(let j=0;j< this.filterData[i].category.length; j++){
if(this.filterData[i].category[j]==id && this.filterData[i].price >=this.rangeValues[0] && this.filterData[i].price <=this.rangeValues[1]){
this.package= this.filterData.filter(item => item.category[j]==id && item.price >=this.rangeValues[0] && item.price <=this.rangeValues[1]);
}
}
}
}else {
for(let i = 0; i < this.filterData.length; i++){
for(let j=0;j<this.filterData[i].category.length; j++){
if(this.filterData[i].category[j]!==id && this.filterData[i].price >=this.rangeValues[0] && this.filterData[i].price <=this.rangeValues[1]){
this.package = this.filterData.filter(item => item.category[j]!== id && item.price >=this.rangeValues[0] && item.price <=this.rangeValues[1]);
}
}
}
}
}
}
我想要实现的目标:
价格过滤器: ,其中我使用了范围滑块来过滤结果,并且可以正确过滤价格,但也应该针对选定的持续时间和类别过滤结果。
持续时间过滤器: 使用该复选框来过滤结果,这应该过滤给定价格和所选类别的结果。
类别过滤器: :一个product_name可以具有一个或多个类别,并且应该过滤选定的一个和所选价格的结果。
答案 0 :(得分:2)
好吧,通过循环遍历并检查它是否可以使您完成一项工作,您使事情变得复杂了。
第一步:制作3个模型
rangeValues: number[] = [0, 2000];
durations: any = [];
themes: any = [];
步骤2 :在这些模型中分配用户的值
handleChange() {
this.ApplyFilters();
}
checkFilter(id) {
if (this.durations.some(a => a === id)) {
this.durations = this.durations.filter(a => a !== id)
} else {
this.durations.push(id)
}
this.ApplyFilters();
}
filterTheme(id) {
if (this.themes.some(a => a === id)) {
this.themes = this.themes.filter(a => a !== id)
} else {
this.themes.push(id)
}
this.ApplyFilters();
}
第3步:创建一个通用过滤器
ApplyFilters() {
this.package = this.filterData.filter(item => {
return (item.price >= this.rangeValues[0] && item.price <= this.rangeValues[1]) && (this.durations.some(b => b === item.duration) || this.durations.length === 0) && (item.category.every(c => this.themes.some(d => d === c)) || this.themes.length === 0)
});
}
您完成了
这里是demo