angular - ReferenceError:customValueFormatter 未定义 ag 网格角度
问题描述
我在一个单独的 JSON 文件中有列定义,我正在尝试将值格式化程序添加到我的一个字段中,并且我正在组件文件中定义值格式化程序函数,但我收到此错误“异常 = ReferenceError:未定义 customValueFormatter”
这是列定义 JSON
[
{
"field": "parent",
"rowGroup": true,
"cellRenderer": "toolTipComponent",
"flex": 1
},
{
"field": "set",
"rowGroup": true,
"cellRenderer": "toolTipComponent",
"flex": 1
},
{
"field": "child",
"rowGroup": true,
"cellRenderer": "toolTipComponent",
"flex": 1
},
{
"field": "Item",
"rowGroup": true,
"cellRenderer": "toolTipComponent",
"flex": 1
},
{
"field": "factName",
"pivot": true
},
{
"headerName": "Value",
"field": "value",
"valueFormatter": "customValueFormatter",
"cellRenderer": "labelComponent",
"aggFunc": "customAggregateFunction",
"flex": "1",
"wrapText": true,
"autoHeight": true
}
]
这是我的组件文件
import { Component, Input, OnInit } from '@angular/core';
import { RightsScreenHttpService } from './services/rights-screen-http.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import { SnackBarComponent } from '../../shared/snack-bar/snack-bar.component';
import { ToolTipComponent } from '../tool-tip/tool-tip.component';
import { LabelComponent } from '../../shared/label-generator';
const rightColumnDefs = require('../../../assets/rightsHeaderNames.json');
@Component({
selector: 'app-rights-screen',
templateUrl: './rights-screen.component.html',
styleUrls: ['./rights-screen.component.scss']
})
export class RightsScreenComponent implements OnInit {
gridApi: any;
gridColumnApi: any;
columnDefs: any[];
defaultColDef: any;
autoGroupColumnDef: any;
rowData: any = [];
isLoading: boolean;
@Input() data: any;
type: string;
frameworkComponents: {
toolTipComponent: any;
labelComponent: any;
};
aggFuncs: any;
getRowStyle: any;
isRowDataEmpty: boolean;
dummyColumnDefs: any;
constructor(private _rightsService: RightsScreenHttpService, private _snakBar: MatSnackBar) {
this.columnDefs = rightColumnDefs;
this.dummyColumnDefs = rightsDummyHeaderNames;
this.defaultColDef = {
filter: true,
resizable: true
};
this.autoGroupColumnDef = {
minWidth: 280,
cellRendererParams: {
suppressCount: true
}
};
this.frameworkComponents = {
toolTipComponent: ToolTipComponent,
labelComponent: LabelComponent
};
}
ngOnInit() {
}
onGridReady(evt) {
this.gridApi = evt.api;
this.gridColumnApi = evt.columnApi;
this.defaultColDef = { resizable: true };
if (this.rowData.length === 0) {
this.isRowDataEmpty = true;
}
}
customValueFormatter(params){
if (params.node.field === 'contentsetItem') {
let childrenValues = [];
let pivotKeys = params.colDef.pivotKeys;
console.log(pivotKeys);
}
}
onCellClicked(evt) {}
onSelectionChanged(evt) {}
}
谁能告诉我我在哪里犯了错误
解决方案
JSON 文件中的"valueFormatter": "customValueFormatter"
条目仅将字符串分配给customValueFormatter
属性valueFormatter
。它不会customValueFormatter(params)
像您希望的那样分配功能。
为此,您必须在组件中本地扩展 JSON 文件中的对象。
this.columnDefs = rightColumnDefs.map(col =>
({ ...col, valueFormatter: (params) => this.customValueFormatter(params) })
);
当然,这会将格式化程序添加到数组的每个对象中。您可以使用三元运算符有条件地添加格式化程序以选择数组的少数对象。
this.columnDefs = rightColumnDefs.map(col =>
// example (add formatter based on the `field` property)
col['field'] === 'value'
? ({ ...col, valueFormatter: (params) => this.customValueFormatter(params) })
: col
);
推荐阅读
- python-3.x - 变量错误地通过 Elif 语句(Python 3)
- firebase - CocoaPods 找不到 pod "Firebase/Database" 的兼容版本:
- openid-connect - 将 mod_auth_openidc 与登录表单集成
- javascript - Fabric JS在数据库中存储步骤
- python - 如何在python中用KNN填充缺失值
- splunk - 使用 API 导出 Splunk 数据
- android - 请求权限创建文件夹外部存储
- visual-studio-code - 如何使用 VS Code 的 sftp 同步扩展同步文件
- three.js - React-three-fiber - 在输入框中键入时禁用键盘控制
- python - 从任务计划程序(在 Windows 中)或 SQL Server 代理运行 os.system(用于命令行)