Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to execute function in Context Menu

I'm trying to call a function in my context menu.

getContextMenuItems(params) {
    console.log(params.node.data)
    var result = [

      {
        name: "Delete",
        action : function () { 
         this.deletePriceFactor(params.node.data);
        }
        ,
        cssClasses: ["redFont", "bold"]
      },
      {
        name: "Audit"
      }

    ]
      return result;
    }

 deletePriceFactor = (rowdata)  =>{
    this.priceFactorService.deleteEntry(rowdata.exchangeCode, rowdata.productCode, rowdata.secType).subscribe(pricefactors => {
    });

  }

I keep getting an error: ERROR TypeError: this.deletePriceFactor is not a function at Object.action (price-factor.component.ts:162)

I have tried using arrow functions like this:

action : () =>  { 
         this.deletePriceFactor(params.node.data);
        }

The above results in another error: core.js:1673 ERROR TypeError: Cannot read property 'deletePriceFactor' of undefined

like image 716
MarkJ Avatar asked Jul 01 '19 15:07

MarkJ


1 Answers

if your html is like:

<ag-grid-angular
      [getContextMenuItems]="getContextMenuItems"
      .
      .
      .
    ></ag-grid-angular>

then the function getContextMenuItems must be writen like :

getContextMenuItems = (params) => {
}

Hence, the this keyword points to your component.

After that, call your method like:

action : () => this.deletePriceFactor(params.node.data)
like image 104
bubbles Avatar answered Oct 25 '22 23:10

bubbles