欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular6 Filter實(shí)現(xiàn)頁面搜索的示例代碼

 更新時(shí)間:2018年12月02日 08:19:58   作者:ValueMar  
這篇文章主要介紹了Angular6 Filter實(shí)現(xiàn)頁面搜索的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前言

我們?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)指正。以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用angular幫你實(shí)現(xiàn)拖拽的示例

    使用angular幫你實(shí)現(xiàn)拖拽的示例

    下面小編就為大家?guī)硪黄褂胊ngular幫你實(shí)現(xiàn)拖拽的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • 詳解angular應(yīng)用容器化部署

    詳解angular應(yīng)用容器化部署

    這篇文章主要介紹了詳解angular應(yīng)用容器化部署,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-08-08
  • Angular開發(fā)者指南之入門介紹

    Angular開發(fā)者指南之入門介紹

    本篇文章主要介紹了Angular開發(fā)者指南的入門知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • Angular利用trackBy提升性能的方法

    Angular利用trackBy提升性能的方法

    這篇文章主要介紹了在Angular中利用trackBy來提升性能的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2018-01-01
  • 深入聊一聊Angular開發(fā)的內(nèi)容

    深入聊一聊Angular開發(fā)的內(nèi)容

    使用Angular開發(fā)需要非常多的前置知識(shí),比如TypeScript、RxJS等,所以學(xué)習(xí)成本比較高,下面這篇文章主要給大家關(guān)于Angular開發(fā)內(nèi)容的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • AngularJS實(shí)現(xiàn)單獨(dú)作用域內(nèi)的數(shù)據(jù)操作

    AngularJS實(shí)現(xiàn)單獨(dú)作用域內(nèi)的數(shù)據(jù)操作

    這篇文章給大家介紹了利用AngularJs如何實(shí)現(xiàn)ng-repeat內(nèi)各個(gè)小的子作用域單獨(dú)數(shù)據(jù)綁定。有需要的小伙伴們可以參考借鑒,下面來一起看看吧。
    2016-09-09
  • AngularJS中ng-options實(shí)現(xiàn)下拉列表的數(shù)據(jù)綁定方法

    AngularJS中ng-options實(shí)現(xiàn)下拉列表的數(shù)據(jù)綁定方法

    今天小編就為大家分享一篇AngularJS中ng-options實(shí)現(xiàn)下拉列表的數(shù)據(jù)綁定方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Angular2下使用pdf插件的方法詳解

    Angular2下使用pdf插件的方法詳解

    這篇文章主要給大家介紹了在Angular2下使用pdf插件的方法,使用這個(gè)插件是要實(shí)現(xiàn)一個(gè)pdf顯示的功能,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • Material(包括Material Icon)在Angular2中的使用詳解

    Material(包括Material Icon)在Angular2中的使用詳解

    這篇文章主要介紹了Material(包括Material Icon)在Angular2中的使用,需要的朋友可以參考下
    2018-02-02
  • AngularJs  Creating Services詳解及示例代碼

    AngularJs Creating Services詳解及示例代碼

    本文主要介紹AngularJs Creating Services的知識(shí)資料,這里整理了詳細(xì)的資料及簡(jiǎn)單示例代碼,有需要的小伙伴可以參考下
    2016-09-09

最新評(píng)論