英文:
Is there a way to listen for changes in p-columnFilter match options and call a function in Angular and PrimeNG?
问题
是否可以绑定一个函数,每当用户在p-columnFilter
的matchOption
之间进行选择时都会调用该函数。
<p-columnFilter type="date">
...
</p-columnFilter>
示例:https://stackblitz.com/edit/owuvzd?file=src%2Fapp%2Fdemo%2Ftable-filter-menu-demo.html
英文:
Is it possible to bind a function that gets called every time user chooses between matchOption
of p-columnFilter
.
<p-columnFilter type="date">
...
</p-columnFilter>
example: https://stackblitz.com/edit/owuvzd?file=src%2Fapp%2Fdemo%2Ftable-filter-menu-demo.html
答案1
得分: 1
我在文档中进行了一些搜索,但没有找到官方的解决方法,所以我想出了这个解决方案,感觉有点像一个 hack(如果你找到另一种方法,请忽略这个)
@ViewChildren(ColumnFilter) columnFilters: QueryList<ColumnFilter>;
ngAfterViewInit() {
this.columnFilters.forEach((columnFilter) => {
const originalFunc = columnFilter.onMenuMatchModeChange;
columnFilter.onMenuMatchModeChange = function (
value: any,
filterMeta: FilterMetadata
) {
originalFunc.call(this, value, filterMeta);
// ... your code goes here
console.log(value, filterMeta);
};
});
}
英文:
I searched a bit through the docs but i didn't see an official way of doing this so I came up with this solution that feels like a hack (if you find another way disregard this)
@ViewChildren(ColumnFilter) columnFilters: QueryList<ColumnFilter>;
ngAfterViewInit() {
this.columnFilters.forEach((columnFilter) => {
const originalFunc = columnFilter.onMenuMatchModeChange;
columnFilter.onMenuMatchModeChange = function (
value: any,
filterMeta: FilterMetadata
) {
originalFunc.call(this, value, filterMeta);
// ... your code goes here
console.log(value, filterMeta);
};
});
}
答案2
得分: 0
我成功找到了一个解决方法,通过创建自己的matchModeOptions
和它们的筛选函数来实现。
为了做到这一点,我利用了FilterService
。
public dateIsFilter: string = "is-date";
public matchModeOptions: any[] = [
{ label: "Date is", value: this.dateIsFilter }
]
constructor(private filterService: FilterService){
}
ngOnInit(): void {
this.filterService.register(this.dateIsFilter, (value: any, filter: any) => {
// 在这里编写代码
return this.filterService.filters.dateIs(value, filter)
});
}
然后,我更改了p-columnFilter
以使用自定义的matchModeOptions
。
<p-columnFilter type="date" [matchModeOptions]="this.matchModeOptions">
...
</p-columnFilter>
英文:
I was able to find a workaround to this, by creating my own matchModeOptions
and their filter functions.
To do this I made use of FilterService
public dateIsFilter: string = "is-date";
public matchModeOptions: any[] = [
{ label: "Date is", value: this.dateIsFilter }
]
constructor(private filterService: FilterService){
}
ngOnInit(): void {
this.filterService.register(this.dateIsFilter, (value: any, filter: any) => {
// code here
return this.filterService.filters.dateIs(value, filter)
});
}
Then I changed p-columnFilter
to use custom matchModeOptions
.
<p-columnFilter type="date" [matchModeOptions]="this.matchModeOptions">
...
</p-columnFilter>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论