2

I need to implement Edit and Delete option in custom component like this below

enter image description here

Component

<lightning:buttonMenu alternativeText="More options" onselect="{!c.lineAction }" iconSize="small" aura:id="editDelete">
  <lightning:menuItem label="Edit" value="1"  />
  <lightning:menuItem label="Delete" value="2" /> 
</lightning:buttonMenu>

Controller script

lineAction:function(component, event, helper){

  // Here I need to identify whether edit or delete selected and its corresponding values

}
6

Update (API Change)

In order to determine which menu item was used, check the event's "id" and "value" parameters to determine which meny item was used:

({
  handleSelect: function(component, event, helper) {
    switch(event.getParam('value')) {
      case "1": helper.doEdit(); break;
      case "2": helper.doDelete(); break;
    }
  }
})

The previous method has been deprecated and may eventually cease to work.


Original Answer

See lightning:menuItem for details, but basically, you can check event.detail.menuItem to determine which menu item was selected.

({
    handleSelect: function(component, event, helper) {
        var menuValue = event.detail.menuItem.get("v.value");
        switch(menuValue) {
            case "1": helper.doEdit(); break;
            case "2": helper.doDelete(); break;
        }
    }
})
| improve this answer | |
  • 1
    Works! To add: I found "Although the event.detail syntax continues to be supported, we recommend that you update your JavaScript code to use the following patterns for the onselect handler as we plan to deprecate event.detail in a future release. - event.getParam("id") - event.getParam("value")" link – Steven.B Jun 29 '18 at 10:43
  • 1
    @Steven.B Thanks for the update. This answer is almost two years old, so it's not surprising that things have changed since. I'll amend this answer. – sfdcfox Jun 29 '18 at 12:55
  • how do salesforce docs not mention this lol – Andy Ray Mar 26 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.