Angular6 Filter實(shí)現(xiàn)頁面搜索的示例代碼
前言
我們?cè)陂_發(fā)過程中經(jīng)常會(huì)遇到在頁面上實(shí)現(xiàn)全局搜索的需求,例如:表格搜索,通過關(guān)鍵詞檢索整個(gè)表格,過濾出我們需要的數(shù)據(jù)。在Angular6 中我們可以通過Filter + Pipe 的方式來實(shí)現(xiàn)這個(gè)功能。下面我們看一下實(shí)現(xiàn)代碼。
經(jīng)人提醒,代碼排版太亂。后續(xù)考慮將一個(gè)完整版的demo放到GitHub上,敬請(qǐng)期待。
實(shí)現(xiàn)代碼
第一步
新建一個(gè)名為 filter.pipe.ts 的文件,這部分是實(shí)現(xiàn)的核心代碼:
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'globalFilter' }) export class GlobalFilterPipe implements PipeTransform { transform(items: any, filter: any, defaultFilter: boolean): any { if (!filter){ return items; } if (!Array.isArray(items)){ return items; } if (filter && Array.isArray(items)) { let filterKeys = Object.keys(filter); if (defaultFilter) { return items.filter(item => filterKeys.reduce((x, keyName) => (x && new RegExp(filter[keyName], 'gmi').test(item[keyName])) || filter[keyName] == "", true)); } else { return items.filter(item => { return filterKeys.some((keyName) => { return new RegExp(filter[keyName], 'gmi').test(item[keyName]) || filter[keyName] == ""; }); }); } } } }
代碼部分的正則表達(dá)式可以根據(jù)需要替換,這里是全局匹配。
第二步
在app.module.ts 文件中導(dǎo)入。
import { GlobalFilterPipe } from './shared/filter.pipe'; registerLocaleData(zh); @NgModule({ declarations: [ GlobalFilterPipe, ]
第三步
在需要的html 文件中應(yīng)用,在 componet 中定義一個(gè)搜索框的變量。
<nz-input-group nzSearch nzSize="large" [nzSuffix]="suffixButton"> <input type="text" [(ngModel)]="searchText" nz-input placeholder="input search text"> </nz-input-group> <ng-template #suffixButton> <button nz-button nzType="primary" nzSize="large" nzSearch>Search</button> </ng-template> <br> <br> <nz-card *ngFor="let topData of topCategoriesData" nzTitle="{{topData.categoryName}}"> <div nz-card-grid [ngStyle]="gridStyle" *ngFor="let secondData of topData.subCategories | globalFilter: {categoryName: searchText,categoryCode: searchText}" > <nz-collapse> <nz-collapse-panel [nzHeader]="secondData.categoryName+'('+secondData.categoryCode+')'" [nzActive]="false" [nzDisabled]="false"> <nz-select style="width: 100%;" (nzOpenChange)="loadMore(secondData.categoryId)" nzPlaceHolder="請(qǐng)選擇..." nzAllowClear> <nz-option *ngFor="let thirdData of thirdCategoriesData | globalFilter: {categoryName: searchText,categoryCode: searchText}" [nzValue]="thirdData.categoryId" [nzLabel]="thirdData.categoryName+'('+thirdData.categoryCode+')'"></nz-option> <nz-option *ngIf="isLoading" nzDisabled nzCustomContent> <i nz-icon type="loading" class="loading-icon"></i> Loading Data... </nz-option> </nz-select> </nz-collapse-panel> </nz-collapse> <!-- <a>{{secondData.categoryName}}</a><b>({{secondData.categoryCode}})</b> --> </div> <ng-template #extraTemplate> <a>二級(jí)分類數(shù)量:{{data.subCategories.length}}</a> </ng-template> </nz-card>
import { Component, OnInit } from '@angular/core'; import { CategoryService } from '../category.service'; @Component({ selector: 'app-category', templateUrl: '../pages/category.component.html', styleUrls: ['../pages/category.component.css'] }) export class CategoryComponent implements OnInit { //todo 搜索無法由下至上匹配1,2級(jí)數(shù)據(jù) public searchText:string; topCategoriesData=[]; thirdCategoriesData=[]; isLoading = false; constructor(private categoryService:CategoryService) { } loadMore(id): void { this.isLoading = true; this.categoryService.getThirdById(id).subscribe((data:any) => { this.isLoading = false; this.thirdCategoriesData=data; }); } ngOnInit():void { this.categoryService.getCategoriesTop().subscribe( (data:any)=>{ this.topCategoriesData = data; }); } }
關(guān)鍵代碼是:globalFilter: {categoryName: searchText,categoryCode: searchText}
其他代碼都是為了完整而貼上去的。
結(jié)語
具體的實(shí)現(xiàn)思路是利用 filter + pipe 在數(shù)據(jù)集中進(jìn)行過濾,因?yàn)檫@里直接過濾的是數(shù)據(jù)集。所以沒辦法單獨(dú)設(shè)置過濾html,然后我遇到的問題是如果在一個(gè)包含有2級(jí)數(shù)據(jù)結(jié)構(gòu)的html中應(yīng)用的話,會(huì)從1級(jí)開始匹配,匹配到2級(jí)再結(jié)束。但如果1級(jí)未匹配到則2級(jí)不再匹配。例如:你的1級(jí)數(shù)據(jù)為:“醫(yī)藥品”,2級(jí)數(shù)據(jù)為“醫(yī)藥部外品”,“外用藥品”。搜索詞為:醫(yī)藥部,則不會(huì)顯示任何結(jié)果。
最后,感謝閱讀。本文如有不對(duì)的地方,還請(qǐng)指正。以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Angular搜索場景中使用rxjs的操作符處理思路
- Angular實(shí)現(xiàn)搜索框及價(jià)格上下限功能
- angularjs實(shí)現(xiàn)分頁和搜索功能
- Angularjs過濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能示例
- angularjs實(shí)現(xiàn)搜索的關(guān)鍵字在正文中高亮出來
- Angular2搜索和重置按鈕過場動(dòng)畫
- 在 Angular 中實(shí)現(xiàn)搜索關(guān)鍵字高亮示例
- Angularjs實(shí)現(xiàn)搜索關(guān)鍵字高亮顯示效果
- Angularjs material 實(shí)現(xiàn)搜索框功能
相關(guān)文章
AngularJS實(shí)現(xiàn)單獨(dú)作用域內(nèi)的數(shù)據(jù)操作
這篇文章給大家介紹了利用AngularJs如何實(shí)現(xiàn)ng-repeat內(nèi)各個(gè)小的子作用域單獨(dú)數(shù)據(jù)綁定。有需要的小伙伴們可以參考借鑒,下面來一起看看吧。2016-09-09AngularJS中ng-options實(shí)現(xiàn)下拉列表的數(shù)據(jù)綁定方法
今天小編就為大家分享一篇AngularJS中ng-options實(shí)現(xiàn)下拉列表的數(shù)據(jù)綁定方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08Material(包括Material Icon)在Angular2中的使用詳解
這篇文章主要介紹了Material(包括Material Icon)在Angular2中的使用,需要的朋友可以參考下2018-02-02AngularJs Creating Services詳解及示例代碼
本文主要介紹AngularJs Creating Services的知識(shí)資料,這里整理了詳細(xì)的資料及簡單示例代碼,有需要的小伙伴可以參考下2016-09-09