【发布时间】:2020-10-17 00:22:10
【问题描述】:
我在 Angular JS 中创建了 DataTable。它工作正常,但现在我正在使用角管实现搜索过滤器。对于这种情况,我创建了以下代码
app.component.ts
import { Component } from '@angular/core';
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Component({
selector: 'app-root',
template: `<input [(model)]="query" type="text" />
<ul>
<li *ngFor="let item of mf.data | search:query" >{{item.name}}</li>
</ul>`,
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private obj: any =[
{ name: "Sinta", email: "sinta@gmail.com", age: "50" },
{ name: "Jojo", email: "Jojo@gmail.com", age: "15" },
{ name: "Andre", email: "Andre@gmail.com", age: "85" } ];
private userlist: any[]=[];
constructor() {
this.userlist=this.obj;
}
}
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { DataTableModule } from "angular2-datatable";
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
DataTableModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
@Pipe({
name: 'search',
pure: false
})
@Injectable()
export class search implements PipeTransform {
transform(item:any[], args:any):any[] {
var isSearch = (data:any): bool => {
var isAll = false;
if(typeof data === 'object' ){
for (var z in data) {
if(isAll = isSearch(data[z]) ){
break;
}
}
} else {
if(typeof args === 'number'){
isAll = data === args;
} else {
isAll = data.toString().match( new RegExp(args, 'i') );
}
}
return isAll;
};
return item.filter(isSearch);
}
}
但是我正在寻找的东西不起作用,任何人都可以帮助构建过滤管道
【问题讨论】:
-
你能解释一下你必须过滤所有字段还是一个字段。
-
我想要名称的搜索过滤器。我的意思是,如果我们搜索一个名称,那么它应该显示名称下的特定用户详细信息