Issue
I have some Angular 13 Code which i am trying to return a column definition which includes a valueGetter which is a function and that's where I run into an issue.
Below is a sample function i like to pass in from my api
getSellerAgentandOffice(params: ValueGetterParams) {
console.log('Calling Value Getter')
return params.data.ListAgentFirstName + ' ' + params.data.ListAgentLastName ;
}
to do so i would use below code to return it
private createColumnDefs(columnsList: IGridColumnAgGrid[] = this.columnsList) {
console.log(this.columnsList)
let valueGetterFunction
this.gridColumnDefs = columnsList.map((column) => {
return {
colId: column.colId,
headerName: column.headerName,
valueGetter: this.getSellerAgentandOffice,
This works fine but i need it more dynamic so i tried the following which creates this error.
valueGetter:this[column.valueGetter]
Based on that i tried a few other things like
let valueGetterFunction
this.gridColumnDefs = columnsList.map((column) => {
if(column.valueGetter ==="getSellerAgentandOffice") {
valueGetterFunction = this[column.valueGetter]
}
valueGetter: this[valueGetterFunction],
this one works but if I get rid of the if(column.valueGetter ==="getSellerAgentandOffice") or make it a if(column.valueGetter !=="getSellerAgentandOffice")
So i am a bit confused on how to fix this issue or if there is even a simple fix other then do a bunch of if's to assign the function.
Here is a working sample that shows the issue Stackblitz Sample Code
Solution
In any case if someone should run into a similar issue below is the code that fix the issue as i guess the error was due to fact that it could be possibly undefined or nothing.
const valueGetter = column.valueGetter as string;
valueGetterFunction = this[valueGetter] || '';
Answered By - MisterniceGuy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.