Angular6 Filter實現(xiàn)頁面搜索的示例代碼
前言
我們在開發(fā)過程中經(jīng)常會遇到在頁面上實現(xiàn)全局搜索的需求,例如:表格搜索,通過關鍵詞檢索整個表格,過濾出我們需要的數(shù)據(jù)。在Angular6 中我們可以通過Filter + Pipe 的方式來實現(xiàn)這個功能。下面我們看一下實現(xiàn)代碼。
經(jīng)人提醒,代碼排版太亂。后續(xù)考慮將一個完整版的demo放到GitHub上,敬請期待。
實現(xiàn)代碼
第一步
新建一個名為 filter.pipe.ts 的文件,這部分是實現(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] == ""; }); }); } } } }
代碼部分的正則表達式可以根據(jù)需要替換,這里是全局匹配。
第二步
在app.module.ts 文件中導入。
import { GlobalFilterPipe } from './shared/filter.pipe'; registerLocaleData(zh); @NgModule({ declarations: [ GlobalFilterPipe, ]
第三步
在需要的html 文件中應用,在 componet 中定義一個搜索框的變量。
<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="請選擇..." 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>二級分類數(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級數(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; }); } }
關鍵代碼是:globalFilter: {categoryName: searchText,categoryCode: searchText}
其他代碼都是為了完整而貼上去的。
結語
具體的實現(xiàn)思路是利用 filter + pipe 在數(shù)據(jù)集中進行過濾,因為這里直接過濾的是數(shù)據(jù)集。所以沒辦法單獨設置過濾html,然后我遇到的問題是如果在一個包含有2級數(shù)據(jù)結構的html中應用的話,會從1級開始匹配,匹配到2級再結束。但如果1級未匹配到則2級不再匹配。例如:你的1級數(shù)據(jù)為:“醫(yī)藥品”,2級數(shù)據(jù)為“醫(yī)藥部外品”,“外用藥品”。搜索詞為:醫(yī)藥部,則不會顯示任何結果。
最后,感謝閱讀。本文如有不對的地方,還請指正。以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
AngularJS實現(xiàn)單獨作用域內的數(shù)據(jù)操作
這篇文章給大家介紹了利用AngularJs如何實現(xiàn)ng-repeat內各個小的子作用域單獨數(shù)據(jù)綁定。有需要的小伙伴們可以參考借鑒,下面來一起看看吧。2016-09-09AngularJS中ng-options實現(xiàn)下拉列表的數(shù)據(jù)綁定方法
今天小編就為大家分享一篇AngularJS中ng-options實現(xiàn)下拉列表的數(shù)據(jù)綁定方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Material(包括Material Icon)在Angular2中的使用詳解
這篇文章主要介紹了Material(包括Material Icon)在Angular2中的使用,需要的朋友可以參考下2018-02-02AngularJs Creating Services詳解及示例代碼
本文主要介紹AngularJs Creating Services的知識資料,這里整理了詳細的資料及簡單示例代碼,有需要的小伙伴可以參考下2016-09-09