简体   繁体   English

AG-Grid(企业)列菜单监听器

[英]AG-Grid (Enterprise) Column Menu Listener

Is there a way to set a listener to column-menu, so that an event is fired when I open and close the menu? 有没有办法将监听器设置为列菜单,以便在打开和关闭菜单时触发事件?

Feature description: https://www.ag-grid.com/javascript-grid-column-menu/ 功能描述: https//www.ag-grid.com/javascript-grid-column-menu/

I already searched in the official documentation, but didn't find an answer. 我已经在官方文档中搜索过,但没有找到答案。

Background is: I want to store the table state with displayed cols, sorting, position of cols, filter etc. in a database. 背景是:我想在数据库中存储具有显示的cols,排序,cols位置,过滤器等的表状态。 Of course I could use the listeners like onFilterChanged , onDisplayedColumnsChanged or onSortChanged . 当然我可以使用onFilterChangedonDisplayedColumnsChangedonSortChanged等监听onSortChanged Problem is, that it will be fired every time when something changes and so there are produced a lot of unwanted api-calls. 问题是,每次发生变化时都会被触发,因此会产生大量不需要的api调用。

Thats why I want to perform one call when the column-menu is closed. 这就是为什么我想在列菜单关闭时执行一个调用。


Update 更新

As Viqas said in his Answer , there is no official way to do it. 正如Viqas在他的回答中所说,没有正式的方法可以做到这一点。 I tried to avoid the solution with postProcessPopup and tried to find a cleaner solution for my problem - to store the table state. 我试图避免使用postProcessPopup的解决方案,并试图找到一个更清洁的解决方案来解决我的问题 - 存储表状态。

For a workaround with a callback when ColumnMenu is closed Viqas Answer is more appropriate. 对于ColumnMenu关闭时回调的变通方法, Viqas Answer更合适。

Notice that this is no workaround for the callback itself - it is just a (possible) solution to store the table state and perform ONE API Call 请注意,这不是回调本身的解决方法 - 它只是一个(可能的)解决方案来存储表状态并执行ONE API调用

I used the ngOnDestory() function of Angular. 我使用了ngOnDestory()函数。

 ngOnDestory(): void { const tableState = { columnState: this.gridOptions.columnApi.getColumnState(), columnGroupState: this.gridOptions.columnApi.getColumnGroupState(), sortState: this.gridOptions.api.getSortModel(), filterState: this.gridOptions.api.getFilterModel(), displayedColumns: this.gridOptions.columnApi.getAllDisplayedColumns() }; // submit it to API } 

You're right, there's no official way to do it. 你是对的,没有正式的方法可以做到这一点。 A workaround could be to detect when the menu is closed yourself. 解决方法可能是检测菜单何时自行关闭。 Ag-grid does provide you the postProcessPopup callback (see here ) which provides the parameter of type PostProcessPopupParams ; Ag-grid确实为你提供了postProcessPopup回调(见这里 ),它提供了PostProcessPopupParams类型的参数; this contains the column menu popup element that is displayed, so you could check when the menu is no longer visible. 这包含显示的列菜单弹出元素,因此您可以检查菜单何时不再可见。

Create a variable to store the columnMenu element in: 创建一个变量来存储columnMenu元素:

columnMenu: any = null;

Store the columnMenu in this variable using the ag-grid event postProcessPopup : 使用ag-grid事件postProcessPopup将columnMenu存储在此变量postProcessPopup

<ag-grid-angular [postProcessPopup]="postProcessPopup"></ag-grid-angular>

this.postProcessPopup = function(params) {
  this.columnMenu = params.ePopup;
}.bind(this);

Then create a listener to detect when the column menu is no longer visible in the dom: 然后创建一个侦听器来检测dom中列列表不再可见的时间:

this.renderer.listen('window', 'click',(e:Event)=>{
      console.log(this.columnMenu)
        const columnMenuIsInDom = document.body.contains(this.columnMenu);

        if (!columnMenuIsInDom && this.columnMenu != null)
        {
          this.columnMenu = null;
        }
    });

This is slightly hacky and a workaround, but I can't think of a better way at the moment. 这有点hacky和一个解决方法,但我现在想不出更好的方法。

Take a look at this Plunker for illustration. 看一下这个 Plunker的插图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM