简体   繁体   English

Angle2中的过滤管

[英]Filter pipes in angular2

在此处输入图片说明 I have 2 select fields,namely Category and Category Type. 我有2个选择字段,即“类别”和“类别类型”。 I need to filter the table based on these contents. 我需要根据这些内容过滤表。 I have written pipe filter function as well. 我也写了管道过滤器功能。

HTML: HTML:

<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding">
      <label style="font-size: 20px;color: #55595c;font-family: 'Roboto', sans-serif">Category</label>
      <ng-select [options]="category" [(ngModel)] = "filter.name" name="name" class='filterDropDown' placeholder="All" [allowClear]="true" notFoundMsg="No Category Found">
      </ng-select>
    </div>
    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 no-padding" style="left:10%">
      <label style="font-size: 20px;color: #55595c;font-family: 'Roboto', sans-serif">Category Type</label>
      <ng-select [options]="catType" [(ngModel)] = "filter.cat_type" class='filterDropDown' placeholder="All" [allowClear]="true" notFoundMsg="No Category Found">
      </ng-select>
    </div>

component.ts: component.ts:

public filter: Categories = new Categories();
export class Categories {
  public _id:any;
  public name:string;
  public cat_type:string;
}

Pipe.ts: Pipe.ts:

export class Categories {
  public _id:any;
  public name:string;
  public cat_type:string;
}

@Pipe({
  name: 'categoryfilter',
  pure: false
})


      export class CategoryPipe implements PipeTransform {
  transform(items: Categories[], filter: Categories): Categories[] {
    if (!items || !filter) {
      return items;
    }
    return items.filter((item: Categories) => this.applyFilter(item, filter));
  } 
  applyFilter(user: Categories, filter: Categories): boolean {
for (let field in filter) {
  if (typeof filter[field] == 'number') {
    console.log("working");
     if (user.name !== filter.name || user._id !== filter._id || user.cat_type !== filter.cat_type) { 
      return false;
    }
  }
}
  return true;
  }
}

I don't get any error, but the table is getting empty if i select item from the list. 我没有任何错误,但是如果我从列表中选择项目,则表将为空。

Be sure your Category object have all properties at run time 确保类别对象在运行时具有所有属性

or 要么

 applyFilter(user: Categories, filter: Categories): boolean {
         if (user.name +"" !== filter.name +"" || user._id+"" !== filter._id+"" || user.cat_type+"" !== filter.cat_type+"") { 
             return false;
           }
        return true;
      }

Based on your input i tried this code and its working for me 根据您的输入,我尝试了此代码及其对我有用

pipe.ts 管道

import { Pipe, PipeTransform } from '@angular/core';
import { Categories } from './model';

@Pipe({
  name: 'categoryfilter',
  pure: false
})
export class CategoryPipe implements PipeTransform {
  transform(items: Categories[], filter: Categories): Categories[] {
    if (!filter) {
      return items;
    }
    const filtered = items.filter(item => {
      if (filter._id === item._id && filter.cat_type === item.cat_type && filter.name === item.name) {
        return item;
      }
    }
    );
    return filtered;
  }
}

model.ts 模型

export class Categories {
  public _id: any;
  public name: string;
  public cat_type: string;
}

component.ts component.ts

import { Component, OnInit } from '@angular/core';
import { Categories } from './model';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';
  categories: Categories[];
  filter: Categories = { _id: 2, cat_type: 'abc', name: 'xyz2' };

  ngOnInit() {
    this.categories = [];
    this.categories.push({ _id: 1, cat_type: 'abc', name: 'xyz1' });
    this.categories.push({ _id: 2, cat_type: 'abc', name: 'xyz2' });
    this.categories.push({ _id: 3, cat_type: 'abc', name: 'xyz3' });
    this.categories.push({ _id: 2, cat_type: 'abc', name: 'xyz2' });
    this.categories.push({ _id: 2, cat_type: 'abc', name: 'xyz2' });
  }
}

component.html component.html

  <table class="table adminTable">
    <thead>
      <th>Name</th>
      <th>Type</th>
      <th>Image</th>
      <th>Action</th>
    </thead>
    <tbody>
      <tr *ngFor="let category of categories | categoryfilter: filter">
        <td>{{category.name}}</td>
        <td>{{category.cat_type}}</td>
        <td>
          test
            <!-- {{category.image}} -->
          <!-- <img class="cat-img" src="{{category.image}}"> -->
          <br>
          <p class="inputimg">
            <input type="file" name="{{category._id}}" id="{{category._id}}" class="inputfile" 
              style='display: none;' />
            <label for="{{category._id}}" class="img_change">Change</label>
          </p>
          <td>
            <button class="iconBtn first" >
              <i class="fa fa-pencil" aria-hidden="true"></i>
            </button>
            <button class="iconBtn second" >
              <i class="fa fa-trash-o" aria-hidden="true"></i>
            </button>
          </td>
      </tr>
    </tbody>
  </table>

no change in html its will be same as yours. html中没有任何变化,它将与您的相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM