使用 ag-grid 中的自定义过滤器进行文本筛选,可以在输入过滤条件时,自动筛选出所需要的文本范围。
示例代码如下:
import { Component } from '@angular/core';
import { IFilterParams, RowNode } from 'ag-grid-community';
@Component({
selector: 'app-text-filter',
template: `
`,
styleUrls: ['./text-filter.component.scss']
})
export class TextFilterComponent implements agGrid.IMenuFilterComp {
public text: string = '';
private params: IFilterParams;
agInit(params: IFilterParams): void {
this.params = params;
}
isFilterActive(): boolean {
return this.text !== '';
}
modelFromFloatingFilter(from: string): any {
return { text: from };
}
modelToFloatingFilter(model: any): string {
return model.text;
}
onChange(): void {
this.params.filterChangedCallback();
}
doesFilterPass(params: IDoesFilterPassParams): boolean {
return this.text.split('').some(letter => params.data.value.includes(letter));
}
}
{
headerName: 'Name',
field: 'name',
filter: 'appTextFilter'
}