Is there a way to listen for changes in p-columnFilter match options and call a function in Angular and PrimeNG?

huangapple go评论66阅读模式
英文:

Is there a way to listen for changes in p-columnFilter match options and call a function in Angular and PrimeNG?

问题

是否可以绑定一个函数,每当用户在p-columnFiltermatchOption之间进行选择时都会调用该函数。

<p-columnFilter type="date">
...
</p-columnFilter>

示例:https://stackblitz.com/edit/owuvzd?file=src%2Fapp%2Fdemo%2Ftable-filter-menu-demo.html

Is there a way to listen for changes in p-columnFilter match options and call a function in Angular and PrimeNG?

英文:

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>

Is there a way to listen for changes in p-columnFilter match options and call a function in Angular and PrimeNG?

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&lt;ColumnFilter&gt;;
        
  ngAfterViewInit() {
    this.columnFilters.forEach((columnFilter) =&gt; {
      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 = &quot;is-date&quot;;

public matchModeOptions: any[] = [
  { label: &quot;Date is&quot;, value: this.dateIsFilter }
]

constructor(private filterService: FilterService){
}

ngOnInit(): void {
  this.filterService.register(this.dateIsFilter, (value: any, filter: any) =&gt; {
  // code here
  return this.filterService.filters.dateIs(value, filter)
});
}

Then I changed p-columnFilter to use custom matchModeOptions.

&lt;p-columnFilter type=&quot;date&quot; [matchModeOptions]=&quot;this.matchModeOptions&quot;&gt;
...
&lt;/p-columnFilter&gt;

huangapple
  • 本文由 发表于 2023年6月1日 19:51:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定