首页
前端开发
如何为列表添加多个过滤器并在 angular 7 中单独取消选中它们?
如何为列表添加多个过滤器并在 angular 7 中单独取消选中它们?
[英] How to add Multiple filter for a list and uncheck them individually in angular 7?
本文介绍了如何为列表添加多个过滤器并在 angular 7 中单独取消选中它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Angular 7 和 3 按钮中使用 ngFor 填充了一个复选框列表来过滤复选框列表,目前我只能过滤一次.也可以单击活动"和任何其他按钮,并且列表应该只过滤那些.最后,我也应该被允许在点击时撤消过滤器.非常感谢.我的代码如下:
我的 ts 文件
import { Component, OnInit } from '@angular/core';@成分({选择器:'app-checkboxexample',templateUrl: './checkboxexample.component.html',styleUrls: ['./checkboxexample.component.scss']})导出类 CheckboxexampleComponent 实现 OnInit {selectedItemsList = [];已检查 ID = [];过滤器:字符串;num1:任何;复选框数据列表 = [{id: 'C001',标签:'摄影',isChecked:假,完成:假},{id: 'C002',标签:'写作',isChecked:假,完成:真实},{id: 'C003',标签:'绘画',isChecked:假,完成:假},{id: 'C004',标签:'针织',isChecked:假,完成:假},{id: 'C011',标签:'跳舞',isChecked:假,完成:真实},{id: 'C005',标签:'园艺',isChecked:假,完成:假},{id: 'C006',标签:'绘图',isChecked:假,完成:真实},{id: 'C007',标签:'健身',isChecked:假,完成:真实},{id: 'C008',标签:'烹饪',isChecked:假,完成:真实},{id: 'C009',标签:'剪贴簿',isChecked:假,完成:假},{id: 'C010',标签:'折纸',isChecked:假,完成:假}]ngOnInit(): 无效 {if (sessionStorage.getItem('screening') != null) {this.selectedItemsList = JSON.parse(sessionStorage.getItem('screening'));this.checkboxesDataList = this.selectedItemsListthis.num1 = JSON.parse(sessionStorage.getItem('manual'))}this.filter = '全部';this.fetchSelectedItems()this.fetchCheckedIDs()}更改选择(){this.fetchSelectedItems()}fetchSelectedItems() {this.selectedItemsList = this.checkboxesDataList.filter((value, index) => {返回值.isChecked});}fetchCheckedIDs() {this.checkedIDs = []this.checkboxesDataList.forEach((value, index) => {如果(值.isChecked){this.checkedIDs.push(value.id);}});}deleteTodo(id: string): void {this.selectedItemsList = this.selectedItemsList.filter(item => item.id !== id);this.checkboxesDataList.forEach(element => {if (element.id == id.toString()) {element.isChecked = false;}});}提交(){this.num1 = ((document.getElementById(manual") as HTMLInputElement).value);sessionStorage.setItem('manual', JSON.stringify(this.num1));sessionStorage.setItem('screening', JSON.stringify(this.checkboxesDataList))}todosFiltered() {if (this.filter === 'all') {返回 this.checkboxesDataList;} else if (this.filter === 'editing') {返回 this.checkboxesDataList.filter(todo => !todo.completed);} else if (this.filter === '完成') {返回 this.checkboxesDataList.filter(todo => todo.completed);}返回 this.checkboxesDataList;}}
我的 html 文件
检查表 <div class="extra-container"><div class="card";样式=宽度:18rem;padding-left:2%;"><ul class="list-group list-group-flush"><li class="list-group-item"><button [ngClass]="{'active': filter === 'all'}";(click)="filter='all'">All</button></li><li class="list-group-item"><button [ngClass]="{'active': filter === 'editing'}";(click)="filter='editing'">Active</button></li><li class="list-group-item"><button [ngClass]="{'active': filter === 'completed'}";(click)="filter='completed'">Completed</button></li>
<div class="row"><div class="col-md-4"><h4>兴趣爱好列表</h4><ul class="checkbox-items"><li *ngFor=let item of todosFiltered().slice(0,7)"><输入类型=复选框"名称="id=""[(ngModel)]=item.isChecked"(change)=changeSelection()">{{item.label}}<li *ngFor="let item of todosFiltered().slice(7,14)"><输入类型=复选框"名称="id=""[(ngModel)]=item.isChecked"(change)=changeSelection()">{{item.label}}
<div class="col-md-4"><h4>所选爱好的数量:{{selectedItemsList.length}}<输入类型=文本"id=手册"值={{num1}}><ul><li *ngFor="let item of selectedItemsList">{{item.label}}<div class="remove-item";(click)=deleteTodo(item.id)">×
<div class="col-md-4"><h4>对象</h4><代码>{{selectedItemsList |json}}<h4>ID数组</h4><code>{{checkedIDs}}</code>
<按钮类型=提交"(click)="onsubmit()">Submit
解决方案
如果你想应用多个过滤器,我会让每个切换更新它自己的过滤器状态.然后您可以将多个过滤器应用于您的数组并返回结果.您的过滤器方法类似于:
todosFiltered() {让 checkboxesToDisplay = this.checkboxesDataList;如果(this.filterActive){checkboxesToDisplay = checkboxesToDisplay.filter(todo => !todo.completed);}如果(this.filterCompleted){checkboxesToDisplay = checkboxesToDisplay.filter(todo => todo.completed);}返回 checkboxesToDisplay;}
然后每个按钮都可以切换这些过滤器,而全部"按钮可以将它们设置为 false.
Active <li class="list-group-item"><button [ngClass]="{'active': filter === 'completed'}";(click)=filterCompleted=!filterCompleted">Completed</button></li>
我做了一些快速更改,应该会让您更接近您正在寻找的行为:
stackblitz >
I have a checkbox list populated using ngFor in Angular 7 and 3 buttons to filter the checkbox list,I am able to filter only once currently.What i want to do it to add multiple filters like if i click complted button i should also be allowed to click Active and any other button and the list should filter only those.Lastly i should be allowed to undo the filters on its clicks as well.THANKS A LOT IN ADVANCE.
My code is below:
My ts file
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-checkboxexample',
templateUrl: './checkboxexample.component.html',
styleUrls: ['./checkboxexample.component.scss']
})
export class CheckboxexampleComponent implements OnInit {
selectedItemsList = [];
checkedIDs = [];
filter: string;
num1: any;
checkboxesDataList = [
{
id: 'C001',
label: 'Photography',
isChecked: false,
completed: false
},
{
id: 'C002',
label: 'Writing',
isChecked: false,
completed: true
},
{
id: 'C003',
label: 'Painting',
isChecked: false,
completed: false
},
{
id: 'C004',
label: 'Knitting',
isChecked: false,
completed: false
},
{
id: 'C011',
label: 'Dancing',
isChecked: false,
completed: true
},
{
id: 'C005',
label: 'Gardening',
isChecked: false,
completed: false
},
{
id: 'C006',
label: 'Drawing',
isChecked: false,
completed: true
},
{
id: 'C007',
label: 'Gyming',
isChecked: false,
completed: true
},
{
id: 'C008',
label: 'Cooking',
isChecked: false,
completed: true
},
{
id: 'C009',
label: 'Scrapbooking',
isChecked: false,
completed: false
},
{
id: 'C010',
label: 'Origami',
isChecked: false,
completed: false
}
]
ngOnInit(): void {
if (sessionStorage.getItem('screening') != null) {
this.selectedItemsList = JSON.parse(sessionStorage.getItem('screening'));
this.checkboxesDataList = this.selectedItemsList
this.num1 = JSON.parse(sessionStorage.getItem('manual'))
}
this.filter = 'all';
this.fetchSelectedItems()
this.fetchCheckedIDs()
}
changeSelection() {
this.fetchSelectedItems()
}
fetchSelectedItems() {
this.selectedItemsList = this.checkboxesDataList.filter((value, index) => {
return value.isChecked
});
}
fetchCheckedIDs() {
this.checkedIDs = []
this.checkboxesDataList.forEach((value, index) => {
if (value.isChecked) {
this.checkedIDs.push(value.id);
}
});
}
deleteTodo(id: string): void {
this.selectedItemsList = this.selectedItemsList.filter(item => item.id !== id);
this.checkboxesDataList.forEach(element => {
if (element.id == id.toString()) {
element.isChecked = false;
}
});
}
onsubmit() {
this.num1 = ((document.getElementById("manual") as HTMLInputElement).value);
sessionStorage.setItem('manual', JSON.stringify(this.num1));
sessionStorage.setItem('screening', JSON.stringify(this.checkboxesDataList))
}
todosFiltered() {
if (this.filter === 'all') {
return this.checkboxesDataList;
} else if (this.filter === 'editing') {
return this.checkboxesDataList.filter(todo => !todo.completed);
} else if (this.filter === 'completed') {
return this.checkboxesDataList.filter(todo => todo.completed);
}
return this.checkboxesDataList;
}
}
My html file
<h1>Checklist</h1>
<div class="extra-container">
<div class="card" style="width: 18rem;padding-left: 2%;">
<ul class="list-group list-group-flush">
<li class="list-group-item"><button [ngClass]="{'active': filter === 'all'}"
(click)="filter='all'">All</button></li>
<li class="list-group-item"><button [ngClass]="{'active': filter === 'editing'}"
(click)="filter='editing'">Active</button></li>
<li class="list-group-item"><button [ngClass]="{'active': filter === 'completed'}"
(click)="filter='completed'">Completed</button></li>
</ul>
</div>
<div class="row">
<div class="col-md-4">
<h4>List of Hobbies</h4>
<ul class="checkbox-items">
<li *ngFor="let item of todosFiltered().slice(0,7)">
<input type="checkbox" name="" id="" [(ngModel)]="item.isChecked"
(change)="changeSelection()">{{item.label}}
</li>
<li *ngFor="let item of todosFiltered().slice(7,14)">
<input type="checkbox" name="" id="" [(ngModel)]="item.isChecked"
(change)="changeSelection()">{{item.label}}
</li>
</ul>
</div>
<div class="col-md-4">
<h4>No of Selected Hobbies: {{selectedItemsList.length}}
<input type="text" id="manual" value={{num1}}> </h4>
<ul>
<li *ngFor="let item of selectedItemsList">{{item.label}}
<div class="remove-item" (click)="deleteTodo(item.id)">
×
</div>
</li>
</ul>
</div>
<div class="col-md-4">
<h4>Object</h4>
<code>
{{selectedItemsList | json}}
</code>
<h4>Array of IDs</h4>
<code>{{checkedIDs}}</code>
</div>
</div>
<button type="submit" (click)="onsubmit()">Submit</button>
解决方案
If you want to apply multiple filters, I would make each toggle update its own filter state. Then you could apply multiple filters to your array and return the result. Your filter method would look something like:
todosFiltered() {
let checkboxesToDisplay = this.checkboxesDataList;
if (this.filterActive) {
checkboxesToDisplay = checkboxesToDisplay.filter(todo => !todo.completed);
}
if (this.filterCompleted) {
checkboxesToDisplay = checkboxesToDisplay.filter(todo => todo.completed);
}
return checkboxesToDisplay;
}
Then each button could toggle those filters and the 'All' button could set them to false.
<li class="list-group-item"><button [ngClass]="{'active': filter === 'all'}"
(click)="filterActive=false;filterCompleted=false">All</button></li>
<li class="list-group-item"><button [ngClass]="{'active': filter === 'editing'}"
(click)="filterActive=!filterActive">Active</button></li>
<li class="list-group-item"><button [ngClass]="{'active': filter === 'completed'}"
(click)="filterCompleted=!filterCompleted">Completed</button></li>
I made some quick changes that should get you closer to the behavior you are looking for:
stackblitz
这篇关于如何为列表添加多个过滤器并在 angular 7 中单独取消选中它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文