Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

javascript - SAPUI5 Open link on Button press

I have a SAPUI5 application and want to open a link if I click on a button.

I get the value of the specific link from an OData call and a normal <Link> control is working fine with the remote data path.

<Link href="{oData>/Url}" target="_blank" text="Click me" />

Tried several ways to open the website with a <Button> instead, but none of them were working for me.

I tried to put a Button inside of it.

<Link target="_blank" href="{oData>/Url}" text="Click me">
  <Button text="Open Website"/> <!-- Error: Cannot add direct child without default aggregation defined for control sap.m.Link -->
</Link>

But I'm getting the above error.

Tried using an HTML link, but it can't deal with the path of the remote data:

<html:a href="{oData>/Url}" target="_blank"><Button text="Open Website"/></html:a>

SAPUI5 app opened in SAP Web IDE with an invalid URL

Then I tried to use the onPress event handler when using a Button and the window.open function:

<Button text="Open Website" press=".onPress" />
{ // In the Controller
  onPress: function () {
    window.open("{oData>/Url}","_blank");
  },
}

But the Controller also can't deal with the path of the remote data resulting in the same invalid URL.

I also read from URLHelper and tried this sample, but I'm unable to add the "value" attribute to the Button.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<Button text="Open Website" press=".openUrl(${oData>/Url}, true)" />
{ // In the Controller
  openUrl: function(url, newTab) {
    // Require the URLHelper and open the URL in a new window or tab (same as _blank):
    sap.ui.require([ "sap/m/library" ], ({URLHelper}) => URLHelper.redirect(url, newTab));
  },
} 

For more information, see:


Alternatively without a controller by requiring the module directly in XML (since UI5 1.69):

<Button xmlns="sap.m"
  xmlns:core="sap.ui.core"
  core:require="{ sapMLib: 'sap/m/library' }" 
  text="Open Website"
  press="sapMLib.URLHelper.redirect(${oData>/Url}, true)"
/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...