0

I have two links (not buttons) that I'm trying to insert in a Lightning component, but having problems firing them. I start with basic HTML <a> elements and give each an onclick attribute:

<div class="slds-float_left">
    <a aura:id="premStButtonId"
        onclick="{! c.handleButtonClick }">Statement</a>
</div>
<div class="slds-float_right">
    <a aura:id="paySumButtonId"
        onclick="{! c.handleButtonClick }">Summary</a>
</div>

In my Controller I'm using a switch statement so I won't have to repeat the bulk of code. But I specify the window.location.href for the link to go to:

({
    handleButtonClick : function(component, event, helper) {
        var buttonSource = event.getSource();
        var buttonId = buttonSource.getLocalId();

        switch(buttonId) {
            case 'premStButtonId':
                window.location.href = '/premSt/default/';
                break;
            case 'paySumButtonId':
                window.location.href = '/paySum/default/';
                break;
        }
    }
})

I'm still very much a newbie with this, so I can't see where this would fail to fire the link.

1
0

The related documentation for this suggests that this only works for SecureComponent, but normal HTML is a SecureElement. As such, a practical way to do this is just use a data attribute:

<a data-id="paySumButtonId" ... />

...

var buttonId = event.target.dataset.id;
2
  • Thanks @sfdcfox. I tried that but it's still not firing. – Matt Smith Mar 18 '19 at 18:07
  • 1
    @MattSmith Can you be more specific? What does "not firing" mean? I tested this in my org, and it works just fine. – sfdcfox Mar 18 '19 at 18:09
0

If you are in simple lighting experience then you would need to use navigateToURL lighting event as below:

var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
  "url": "/006/o"
});
urlEvent.fire();

Ref : https://developer.salesforce.com/docs/component-library/bundle/force:navigateToURL/documentation

In Lighting console you should use workspace api, code would like as :

    {(
      openTab : function(component, event, helper) {
        var workspaceAPI = component.find("workspace");
        workspaceAPI.openTab({
            url: '#/sObject/001R0000003HgssIAC/view',
            focus: true
        });
      },
    )}

Ref: https://developer.salesforce.com/docs/component-library/bundle/lightning:workspaceAPI/documentation

2
  • From question main problem op seems to be having is to get the button which was clicked and not about navigation. – Raul Mar 18 '19 at 21:19
  • @Raul sfdcfox has asked clarifying question, I think we would have to wait till Matt responds. – Kiran Machhewar Mar 18 '19 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.