简体   繁体   中英

Storing ag-grid rowclicked event data Angular

I am trying to store the event data from the onRowClicked event in a Component member. So that when the user hits a button it will be deleted. However when I try accessing it from the delete callback the member variable is undefined.

export class OilTypesComponent implements OnInit {
 ...
 selectedOil : any;
 gridOptions: GridOptions = <GridOptions>{};

 ngOnInit() {
 this.gridOptions = {
      ...
      onCellEditingStopped: this.cellEdited,
      onRowClicked: this.rowClicked
    }
 }
...
 rowClicked(event){
    this.selectedOil = event.data;
 }

delete(){
    console.log(`Deleting ${this.selectedOil.manufacturer} //this.selectedOil is undefined
  }

Turns out it was a scoping issue when passing in the callbacks as shown here: Angular2 component's "this" is undefined when executing callback function

What I ended up doing for both callbacks

this.gridOptions = {
  ...
  onCellEditingStopped: this.cellEdited.bind(this),
  onRowClicked: this.rowClicked.bind(this)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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