为什么我的过滤在 mat table 中不起作用?
why my filtering not working in mat table?
我的过滤在垫子上不起作用 table。数据来自 api 但我的代码不起作用
我认为我加载的信息类型有双重搜索函数 getFormsValue() 和 formSubscribe
的问题
我的api-service.service.ts是
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http'
export interface Element {
id: number;
name: string;
username: string;
email: string;
}
@Injectable({
providedIn: 'root'
})
export class ApiServiceService {
constructor(private http: HttpClient ) { }
getRandomUsers(): Observable<Element> {
const URL = 'https://jsonplaceholder.typicode.com/users';
return this.http.get<Element>(URL);
}
}
我的代码中的 persons.ts 文件是
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup} from '@angular/forms'
import { FormControl, } from '@angular/forms'
import { MatTable, MatTableDataSource } from '@angular/material/table';
import {ApiServiceService,Element} from './api-service.service'
import { Subscription } from 'rxjs/internal/Subscription';
@Component({
selector: 'app-persons',
templateUrl: './persons.component.html',
styleUrls: ['./persons.component.css']
})
export class PersonsComponent implements OnInit {
private subs = new Subscription();
private dataArray: any;
dataSource = new MatTableDataSource<Element>()
constructor(private financeService: ApiServiceService) { }
displayedColumns: string[] = ['position', 'name', 'username', 'email','action'];
// none value
filterValues = {
name: ''
};
// form group
filterForm = new FormGroup({
name: new FormControl()
});
get name() {
return this.filterForm.get('name');
}
ngOnInit() {
this.subs.add(this.financeService.getRandomUsers()
.subscribe((res) => {
console.log(res);
this.dataArray = res;
console.log(this.dataArray);
this.dataSource = new MatTableDataSource<Element>(this.dataArray)
},
));
this.formSubscribe();
this.getFormsValue();
}
// form subscribe
formSubscribe() {
if (this.name !=null){
this.name.valueChanges.subscribe(nameValue => {
this.filterValues['name'] = nameValue;
this.dataSource.filter = JSON.stringify(this.filterValues);
});
}
}
// create filter
getFormsValue() {
this.dataSource.filterPredicate = (data: any, filter: string): boolean => {
let searchString = JSON.parse(filter);
const resultValue =
data.name.toString().trim().toLowerCase()
.indexOf(searchString.name.toLowerCase()) !== -1;
return resultValue;
};
this.dataSource.filter = JSON.stringify(this.filterValues);
}
}
我的 html 文件如下:
<form [formGroup]="filterForm">
<div>
<div class="form-group">
</div>
<div>
<mat-form-field>
<input matInput formControlName="name" placeholder="Enter name">
</mat-form-field>
</div>
</div>
</form>
<mat-table #table [dataSource]="dataSource" class="mat-elevation-z8">
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<ng-container matColumnDef="username">
<th mat-header-cell *matHeaderCellDef> username </th>
<td mat-cell *matCellDef="let element"> {{element.username}} </td>
</ng-container>
<ng-container matColumnDef="email">
<th mat-header-cell *matHeaderCellDef> email </th>
<td mat-cell *matCellDef="let element"> {{element.email}} </td>
</ng-container>
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef> select</th>
<td mat-cell *matCellDef="let element"> <mat-checkbox >{{element.id}}</mat-checkbox> </td>
*matCellDef="let element"
</ng-container>
<ng-container matColumnDef="action">
<th mat-header-cell *matHeaderCellDef> Action </th>
<td mat-cell *matCellDef="let element" class="action-link">
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</mat-table>
<mat-paginator [length]="10" [pageSizeOptions]="[2,5, 7, 10]" aria-label="Select page">
</mat-paginator>
但我用名字搜索无效
感谢您的帮助
这里的问题是您为 MatTableDataSource
创建了一个自定义过滤器,但一段时间后您用 MatTableDataSource
的新实例覆盖了现有的 dataSource
但不会再次修补过滤器。
this.subs.add(this.financeService.getRandomUsers()
.subscribe((res) => {
console.log(res);
this.dataArray = res;
console.log(this.dataArray);
this.dataSource = new MatTableDataSource<Element>(this.dataArray);
this.getFormsValue(); <========================================= add this
},
));
我的过滤在垫子上不起作用 table。数据来自 api 但我的代码不起作用 我认为我加载的信息类型有双重搜索函数 getFormsValue() 和 formSubscribe
的问题我的api-service.service.ts是
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http'
export interface Element {
id: number;
name: string;
username: string;
email: string;
}
@Injectable({
providedIn: 'root'
})
export class ApiServiceService {
constructor(private http: HttpClient ) { }
getRandomUsers(): Observable<Element> {
const URL = 'https://jsonplaceholder.typicode.com/users';
return this.http.get<Element>(URL);
}
}
我的代码中的 persons.ts 文件是
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormGroup} from '@angular/forms'
import { FormControl, } from '@angular/forms'
import { MatTable, MatTableDataSource } from '@angular/material/table';
import {ApiServiceService,Element} from './api-service.service'
import { Subscription } from 'rxjs/internal/Subscription';
@Component({
selector: 'app-persons',
templateUrl: './persons.component.html',
styleUrls: ['./persons.component.css']
})
export class PersonsComponent implements OnInit {
private subs = new Subscription();
private dataArray: any;
dataSource = new MatTableDataSource<Element>()
constructor(private financeService: ApiServiceService) { }
displayedColumns: string[] = ['position', 'name', 'username', 'email','action'];
// none value
filterValues = {
name: ''
};
// form group
filterForm = new FormGroup({
name: new FormControl()
});
get name() {
return this.filterForm.get('name');
}
ngOnInit() {
this.subs.add(this.financeService.getRandomUsers()
.subscribe((res) => {
console.log(res);
this.dataArray = res;
console.log(this.dataArray);
this.dataSource = new MatTableDataSource<Element>(this.dataArray)
},
));
this.formSubscribe();
this.getFormsValue();
}
// form subscribe
formSubscribe() {
if (this.name !=null){
this.name.valueChanges.subscribe(nameValue => {
this.filterValues['name'] = nameValue;
this.dataSource.filter = JSON.stringify(this.filterValues);
});
}
}
// create filter
getFormsValue() {
this.dataSource.filterPredicate = (data: any, filter: string): boolean => {
let searchString = JSON.parse(filter);
const resultValue =
data.name.toString().trim().toLowerCase()
.indexOf(searchString.name.toLowerCase()) !== -1;
return resultValue;
};
this.dataSource.filter = JSON.stringify(this.filterValues);
}
}
我的 html 文件如下:
<form [formGroup]="filterForm">
<div>
<div class="form-group">
</div>
<div>
<mat-form-field>
<input matInput formControlName="name" placeholder="Enter name">
</mat-form-field>
</div>
</div>
</form>
<mat-table #table [dataSource]="dataSource" class="mat-elevation-z8">
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> No. </th>
<td mat-cell *matCellDef="let element"> {{element.id}} </td>
</ng-container>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<ng-container matColumnDef="username">
<th mat-header-cell *matHeaderCellDef> username </th>
<td mat-cell *matCellDef="let element"> {{element.username}} </td>
</ng-container>
<ng-container matColumnDef="email">
<th mat-header-cell *matHeaderCellDef> email </th>
<td mat-cell *matCellDef="let element"> {{element.email}} </td>
</ng-container>
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef> select</th>
<td mat-cell *matCellDef="let element"> <mat-checkbox >{{element.id}}</mat-checkbox> </td>
*matCellDef="let element"
</ng-container>
<ng-container matColumnDef="action">
<th mat-header-cell *matHeaderCellDef> Action </th>
<td mat-cell *matCellDef="let element" class="action-link">
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</mat-table>
<mat-paginator [length]="10" [pageSizeOptions]="[2,5, 7, 10]" aria-label="Select page">
</mat-paginator>
但我用名字搜索无效 感谢您的帮助
这里的问题是您为 MatTableDataSource
创建了一个自定义过滤器,但一段时间后您用 MatTableDataSource
的新实例覆盖了现有的 dataSource
但不会再次修补过滤器。
this.subs.add(this.financeService.getRandomUsers()
.subscribe((res) => {
console.log(res);
this.dataArray = res;
console.log(this.dataArray);
this.dataSource = new MatTableDataSource<Element>(this.dataArray);
this.getFormsValue(); <========================================= add this
},
));