1

I am using ag-grid with angular 2.

I have created a custom context menu that has delete and update buttons. In a scenario, the user select one or more rows and then right clicks and clicks delete or update button.

These buttons call some functions those handles with gridOptions in order to get selected rows.

However; when I click the delete or update buttons, I have an error that says this.gripOptions is undefined.

Is there any example or documentation about this? How can I overcome this problem?

Thanks for the replies

    var gridOptions = {
        columnDefs: columnDefs,
        enableRangeSelection: true,
        getContextMenuItems: getContextMenuItems,
        allowContextMenuWithControlKey: true
    };
    getContextMenuItems(params) {
        var result = [
            { // custom item
                name: 'Delete',
                action: function () { this.delete()); }
            } 

    return result;
    }

    delete() {
        var selectedRows = this.gridOptions.api.getSelectedRows();
    }
Emad
  • 769
  • 9
  • 21
Şahin Taşın
  • 123
  • 2
  • 10
  • can you add some code snippets to help debug or are you just looking for a general source? – Surreal Aug 02 '17 at 15:05
  • Sure, I can add come code however; it seems that it is a general problem with ag-grid. As far as I understand, the custom context menu is prepared and cannot get in contact with any other outside code. I have tried to use a variable that I defined in .ts but again, error says that variable is undefined. – Şahin Taşın Aug 02 '17 at 15:12

1 Answers1

-1

This is because you declared gridOptions as a variable, not as a part of this. What you can do is:

var vm = this;
vm.gridOptions={...}

[...]

 delete() {
    var selectedRows = vm.gridOptions.api.getSelectedRows();
}

In this way you don't relate on "this", that is difficult to manage in Javascript, but you have a certain reference to the local context

Igino Boffa
  • 411
  • 2
  • 6
  • 26
  • thanks for the answer! I solved my problem with this answer; https://stackoverflow.com/a/45374506/7826844 However; your answer is also correct! – Şahin Taşın Aug 03 '17 at 10:50