Angular ag-grid自定义聚合函数是否可以调用另一个函数?

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

Angular ag-grid customized aggregation function cannot call another function?

问题

Here is the translated code:

我对此感到困惑。我在HTML中有一个Ag-Grid:

<ag-grid-angular
id="data_grid"
title="一些数据"
class="ag-theme-alpine ag-theme-material"
style="width: 2800px; height: 800px; margin-right: 1%; float: left; font-size: 16px !important"
(gridReady)="onDataGridReady($event)"
[rowData]="row_data"
[columnDefs]="column_defs"
[defaultColDef]="defaultColDef"
[autoGroupColumnDef]="autoGroupColumnDef"
[groupDefaultExpanded]="groupDefaultExpanded"
[animateRows]="true"
[gridOptions]="grid_options">
</ag-grid-angular>

然后在组件文件中:
```typescript
public defaultColDef: ColDef = {
    flex: 1,
    minWidth: 100,
    sortable: true,
    resizable: true,
  };
  public autoGroupColumnDef: ColDef = {
    minWidth: 200,
  };
  public groupDefaultExpanded = 1;
  public grid_options = {
    suppressAggFuncInHeader : true
  }
column_defs = [
    {headerName: &#39;A&#39;, field: &#39;many_As&#39;, rowGroup: true },
    {headerName: &#39;B&#39;, field: &#39;many_Bs&#39;, width: 250, aggFunc: this.agg_func },
  ];

agg_func(params: any) {
    ...
    // 如果将此行注释掉,网格将不会被绘制
    another_func(); 
    ...
}

another_func() {
    console.log(&#39;你好?&#39;);
}

现在的问题是,如果我让agg_func调用another_func,网格将不会被绘制,而且这个聚合函数根本不会被调用。

我在这里错过了什么?


Please note that I've only provided the translation of the code portions as requested. If you have further questions or need assistance with the code, please feel free to ask.

<details>
<summary>英文:</summary>

I am confused by this. I have a ag-grid in html:

<ag-grid-angular
id="data_grid"
title="Some Data"
class="ag-theme-alpine ag-theme-material"
style="width: 2800px; height: 800px; margin-right: 1%; float: left; font-size: 16px !important"
(gridReady)="onDataGridReady($event)"
[rowData]="row_data"
[columnDefs]="column_defs"
[defaultColDef]="defaultColDef"
[autoGroupColumnDef]="autoGroupColumnDef"
[groupDefaultExpanded]="groupDefaultExpanded"
[animateRows]="true"
[gridOptions]="grid_options">
</ag-grid-angular>

Then in component file:

public defaultColDef: ColDef = {
flex: 1,
minWidth: 100,
sortable: true,
resizable: true,
};
public autoGroupColumnDef: ColDef = {
minWidth: 200,
};
public groupDefaultExpanded = 1;
public grid_options = {
suppressAggFuncInHeader : true
}
column_defs = [
{headerName: 'A', field: 'many_As', rowGroup: true },
{headerName: 'B', field: 'many_Bs', width: 250, aggFunc: this.agg_func },
];

agg_func(params: any) {
...
another_func(); // the grid gets painted only if this line is commented out
...
}

another_func() {
console.log('Hello?');
}

Now the problem is, if I let agg_func to call another_func, the grid will not be painted and this aggregation function does not get called at all.

What am I missing here?

</details>


# 答案1
**得分**: 1

这很可能是与“this”引用有关的问题。

这些函数是回调函数,从外部的JavaScript代码中调用,所以引用最终会变得不正确。

当将TypeScript类方法传递到外部用作回调时,始终使用“箭头函数”,这样可以捕获“this”引用。

例如,更改为:

```typescript
const agg_func = (params: any) => {
    another_func(); 
}

const another_func = () => {
    console.log('Hello?');
}
英文:

It's most likely a problem with the "this" reference.

The functions are callbacks, being called from JavaScript code external to your class, so the reference winds up being incorrect.

When passing typescript class methods to external use as a callback, always use "arrow functions", which capture the "this" reference.

For example, change to:

const agg_func = (params: any) =&gt; {
    another_func(); 
}

const another_func = () =&gt; {
    console.log(&#39;Hello?&#39;);
}

huangapple
  • 本文由 发表于 2023年5月18日 05:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276297.html
匿名

发表评论

匿名网友

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

确定