代码之家  ›  专栏  ›  技术社区  ›  Red Cricket

在Angular6中编写自定义表格过滤器

  •  0
  • Red Cricket  · 技术社区  · 7 年前

    https://www.code-sample.com/2018/07/angular-6-search-filter-pipe-table-by.html

    它工作得很好,这里是管道代码:

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'gridFilter'
    })
    
    /*
     I got this code from here:
     https://www.code-sample.com/2018/07/angular-6-search-filter-pipe-table-by.html
    */
    
    export class GridFilterPipe 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], 'gi').test(item[keyName])) || filter[keyName] == "", true));
          }
          else {
            return items.filter(item => {
              return filterKeys.some((keyName) => {
                return new RegExp(filter[keyName], 'gi').test(item[keyName]) || filter[keyName] == "";
              });
            });
          }
        }
      }
    }
    

    它可以很好地满足我的大部分需求,除了这个html表格:

      <div class="col-md-4" *ngIf="element == 'location'">
        <div class="spacer"></div>
        <div class="panel panel--vibblue panel--raised">{{ element | titlecase }}</div>
        <div class="responsive-table panel--raised">
          <table class="table table--bordered table--hover approvals-table" id="location-table">
            <thead>
            <tr>
              <th class="sortable">{{ element | titlecase }} Name <span class="sort-indicator icon-chevron-down"></span></th>
              <th class="sortable">Site <span class="sort-indicator icon-chevron-down"></span></th>
              <th>Action</th>
            </tr>
            </thead>
            <tbody>
            <ng-container *ngFor="let el of elements | gridFilter: {name: searchText, site:searchText}">
              <tr>
                <td>{{el.name}}</td>
                <td>{{ getSiteName(el.site) }}</td>
                <td>
                  <a><span class="icon-trash" (click)="deleteElement(el.id, el.name)"></span></a>
                </td><td>
                <a><span class="icon-pencil" (click)="editElement(el)"></span></a>
              </td>
              </tr>
            </ng-container>
            </tbody>
          </table>
        </div>
      </div>
    

    上表的问题是这个。要显示我使用此html的位置的站点名称。。。

    <td>{{ getSiteName(el.site) }}</td>
    

    ... 它调用一个方法 getSiteName id 返回站点的名称。我不知道如何设置 gridFilter

    这是我的stackblitz: https://stackblitz.com/edit/angular-damcul

    **更新**

    我已经更新了我的 stackblitz 斯塔克布利茨 应用程序:

    enter image description here

    我可以搜索位置,这是我搜索“loc_2”的屏幕截图:

    enter image description here

    但我不能搜索网站。这是我搜索任何“网站”的截图。

    enter image description here

    0 回复  |  直到 7 年前
        1
  •  2
  •   Akber Iqbal    7 年前

    做了一些更改:

    • 在问题中的共享代码中,使用了名为 grdFilter
    • 实施 getSiteName 已添加,它接受该对象并返回博客名称(我们的数据中没有名为“site”的属性)
    • 在你的共享代码中

    <ng-container *ngFor="let el of customerData | gridFilter: {name: searchText, site:searchText}">

    • 由于我们的数据中没有名为“site”的属性,我们将其更改为

    <ng-container *ngFor="let el of customerData | grdFilter: {name: searchText, blog:searchText}">

    你好,康普内特

    import { Component, Input } from '@angular/core';
    
    @Component({
      selector: 'hello',
      template: `<input [(ngModel)]="searchText" placeholder="Search.." class="advancedSearchTextbox">
    <p></p>
    <table *ngFor="let emp of customerData | grdFilter: {name: searchText, Age:searchText,  blog: searchText}; let i=index;">
      <tr>
        <td style="width: 5%;">{{i +1}}</td>
        <td style="width: 10%;">{{emp.name}}</td>
        <td style="width: 5%;">{{emp.Age}}</td>
        <td style="width: 15%;">{{emp.blog}}</td>
      </tr>
    </table>
    <hr/>
    <table class="table table--bordered table--hover approvals-table" id="location-table">
      <thead>
        <tr>
          <th class="sortable">{{ element | titlecase }} Name <span class="sort-indicator icon-chevron-down"></span></th>
          <th class="sortable">Site <span class="sort-indicator icon-chevron-down"></span></th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <ng-container *ngFor="let el of customerData | grdFilter: {name: searchText, blog:searchText}">
          <tr>
            <td>{{el.name}}</td>
            <td>{{ getSiteName(el) }}</td>
            <td>
              <a><span class="icon-trash" (click)="deleteElement(el.id, el.name)"></span></a>
            </td><td>
            <a><span class="icon-pencil" (click)="editElement(el)"></span></a>
          </td>
          </tr>
        </ng-container>
      </tbody>
    </table>
    `,
      styles: [`h1 { font-family: Lato; }`]
    })
    export class HelloComponent {
      public searchText: string;
      public customerData: any;
    
      @Input() name: string;
    
      constructor() { }
    
      ngOnInit() {
        this.customerData = [
          { "name": 'Anil kumar', "Age": 34, "blog": 'https://code-view.com' },
          { "name": 'Sunil Kumar Singh', "Age": 28, "blog": 'https://code-sample.xyz' },
          { "name": 'Sushil Singh', "Age": 24, "blog": 'https://code-sample.com' },
          { "name": 'Aradhya Singh', "Age": 5, "blog": 'https://code-sample.com' },
          { "name": 'Reena Singh', "Age": 28, "blog": 'https://code-sample.com' },
          { "name": 'Alok Singh', "Age": 35, "blog": 'https://code-sample.com' },
          { "name": 'Dilip Singh', "Age": 34, "blog": 'https://code-sample.com' }];
      }
      getSiteName(passedObj) {
        return passedObj.blog;
      }
    }
    

    更新#1

    如果转到grd-pipe.ts文件,并执行 console.log(items,filter) ,您将看到我们在自定义管道中处理的数组是locations数组,它的“sites”列值为1、2或3。从sites数组中为UI获取的站点名称不是locations数组的一部分,因此管道无法处理该名称。

    因此,我们在locations数组中创建一个新的字段site name,在其中存储站点名称(来自sites数组),现在由于该字段在管道中可用,管道也可以对其进行搜索。

    TS公司

    ngOnInit(){
    for(var i=0;i<this.locations.length;i++){
            this.locations[i].siteName = this.sites.find(s => s.id === this.locations[i].site).name;
          }
    }
    

    相关变更 添加到筛选器的字段siteName:

    <table class="table table--bordered table--hover approvals-table" id="location-table">
      <tbody>
        <ng-container *ngFor="let loc of locations | grdFilter: {name: searchText, siteName:searchText}; let i=index">
          <tr>
            <td style="width: 5%;">{{i +1}}</td>
            <td style="width: 10%;">{{loc.name}}</td>
            <td style="width: 5%;">{{getSiteName(loc.site)}}</td>
          </tr>
        </ng-container>
      </tbody>
    </table>
    

    工作 stackblitz here 也会更新

    推荐文章