简体   繁体   English

ag-grid cell-renderer 组件在数据更新时失去点击能力

[英]ag-grid cell-renderer component loses its click-ability when data are updated

I'm having a simple angular component that implements an ag-grid like that:我有一个简单的角度组件,它实现了这样的 ag-grid:

<ag-grid-angular
  *ngIf="data"
  domLayout='autoHeight'
  class="ag-theme-material"
  suppressFieldDotNotation="true"
  [pagination]="true"
  [paginationPageSize]="5"
  [defaultColDef]="defaultColDef"
  [gridOptions]="gridOptions"
  [frameworkComponents]="frameworkComponents"
  [rowData]="data.rowData"
  [columnDefs]="data.columnDefs"
  (gridReady)="onGridReady($event)"
  (cellKeyPress)="onCellKeyPress($event)">
</ag-grid-angular>

Then I'm using this component inside other components where I need to display grid-able data.然后我在需要显示可网格数据的其他组件中使用此组件。

<shared-grid [data]="projectStore.datasetsGridData | async" (action)="onDatasetsTableAction($event)"></shared-grid>

As you can see I'm using an observable to set grid's columnDefs and rowData .如您所见,我正在使用 observable 来设置网格的columnDefsrowData

The grid is configured to use a cellRenderer component that implements a clickable element in one of the columns like:网格配置为使用cellRenderer组件,该组件在以下列之一中实现可点击元素:

<div (click)="doSomething()">...</div>

I noticed that for those grids that the data ( [data] input ) continuously change the cell with the custom clickable div loses its clickability as long as the data changes.我注意到,对于那些数据( [data] input )不断更改具有自定义可点击 div 的单元格的网格,只要数据发生变化,就会失去其可点击性。 When new data stop coming up the div is clickable again.当新数据停止出现时,div 可以再次点击。

I'm thinking that this is happening due to continuous refreshing of the grid but I'm not 100% sure.我认为这是由于网格不断刷新而发生的,但我不是 100% 确定。 If that's the case is there any workaround for this effect to stop happening?如果是这种情况,是否有任何解决方法可以阻止这种影响发生?

There are 2 possible solutions to this issue.此问题有 2 种可能的解决方案。

  1. You can implement the refresh() method in your cellRenderer component and handle the refresh logic so that cell renderer component knows what to do if there is data refresh.您可以在 cellRenderer 组件中实现refresh()方法并处理刷新逻辑,以便单元渲染器组件知道如果有数据刷新该做什么。 Here is an example - Handling refresh这是一个示例 - 处理刷新
    Before you do that, there are multiple reasons why refresh is called on cell renderer, if you are not using the preferred api method to set data, then refresh is not called.在执行此操作之前,在单元格渲染器上调用 refresh 的原因有多种,如果您没有使用首选的 api 方法来设置数据,则不会调用 refresh。 You will have to manually invoke api.refreshCells() every time data is refreshed for above solution to每次为上述解决方案刷新数据时,您都必须手动调用api.refreshCells()
    work.工作。 More on this here - Events causing refresh更多关于这里 - 导致刷新的事件

  2. If solution 1 is cumbersome, then the easy way out would be to use api.redrawRows() each time your data changes.如果解决方案 1 很麻烦,那么简单的方法是在每次数据更改时使用api.redrawRows() Think ngOnChanges .想想ngOnChanges Redraw rows recreates the entire DOM of row and applies cell renderer again as if row is newly created.重绘行重新创建行的整个 DOM 并再次应用单元格渲染器,就像新创建的行一样。 As per docs -根据文档 -

Use redraw row if you want to create the row again from scratch.如果您想从头开始再次创建行,请使用重绘行。 This is useful when you have changed a property that only gets used when the row is created for the first time such as:当您更改了仅在第一次创建行时使用的属性时,这很有用,例如:

  1. Whether the row is fullWidth or not.该行是否为全宽。
  2. The cellRenderer used for any cell (as this is specified once when the cell is created).用于任何单元格的 cellRenderer(因为这是在创建单元格时指定的一次)。
  3. You want to specify different styles for the row via the callbacks您想通过回调为行指定不同的样式
    getRowStyle() or getRowClass(). getRowStyle() 或 getRowClass()。

Example - Redraw rows示例 - 重绘行

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

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