cancel
Showing results for 
Search instead for 
Did you mean: 

How to pass dynamic data to sap.m.link sapui5

meenakshi_raina
Advisor
Advisor
0 Kudos

Hi All,

I have few doubts in using SAP.M.LINK.

Through my oData, I am passing multiple links/URLs to my front end. These links will be available under "Attachments" label.

I am getting the value of my 1st link as -

var Link1 = sItem.getBindingContext().getProperty("Link1");

and similary...

var Link2 = sItem.getBindingContext().getProperty("Link2"); and other links.

I tried using the below syntax, but it does not navigate to my URL(even when Link1 has correct value). When I hard coded, a URL in my HREF property, it got successfully navigated to that URL. I am not able to find an error in debug mode.

Please help me with what I am doing wrong.

Thanks

Meenakshi

Accepted Solutions (1)

Accepted Solutions (1)

bpawanchand
Active Contributor
0 Kudos

Hi

Please find below the view definition. I guess in your case the model which your binidng is more important. I created and XML view

<mvc:View controllerName="mysap.ui5.demo.walk.controller.App" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc"
	xmlns="sap.m" xmlns:u="sap.ui.unified">
	<App>
		<pages>
			<Page title="{i18n>title}">
				<content>
					<!--<u:Currency value="2300.12" currency="INR" useSymbol="true" />-->
					<Text text="{ path: '/number', type: 'sap.ui.model.type.Currency', formatOptions: { showMeasure: false } }"/>
					<!--<Text text="Hello World"/>-->
					<Link id="Test" text="Link1" href="{path: '/link1'}"></Link>
					<Link id="Test2" text="Link2" href="{path: '/link2'}"></Link>
				</content>
			</Page>
		</pages>
	</App>
</mvc:View>

The controller definition is as follows

sap.ui.define([
	"sap/ui/core/mvc/Controller", "sap/ui/model/json/JSONModel",
	"sap/ui/core/format/NumberFormat"
], function(Controller, JSONModel, NumberFormat) {
	"use strict";


	return Controller.extend("mysap.ui5.demo.walk.controller.App", {
		_data: {
			"number": ["9999.99"],
			"link1" : 'https://www.google.com',
			"link2" : 'https://www.aol.com'
		},


		onInit: function(evt) {
			var oModel = new JSONModel(this._data);
			this.getView().setModel(oModel);
			var oFmtSet =  NumberFormat.getCurrencyInstance();
			// var oLocale = oFmtSet.getCurrencyInstance();
		}
	});


});

Thanks,

Pavan

meenakshi_raina
Advisor
Advisor
0 Kudos

Hi Pavan,

Thanks for your reply. I tried to use the below code, but it did not work -

bpawanchand
Active Contributor
0 Kudos

Hi,

For me, it looks fine irrespective of the view type. As I mentioned earlier the model which you have bound to the view could be making the difference.

sap.ui.jsview("mysap.ui5.sandappSandApplication.view.Main", {
	getControllerName: function() {
		return "mysap.ui5.sandappSandApplication.controller.Main";
	},
	createContent: function(oController) {
		var oPage = new sap.m.Page({
			title: "{i18n>title}",
			content: [
				new sap.m.Link({
						id: "link1", // sap.ui.core.ID
						text: "Link1", // string
						href: "{path: '/link1'}" // sap.ui.core.URI 
					}
				),
				new sap.m.Link({
						id: "link2", // sap.ui.core.ID
						text: "Link2", // string
						href: "{path: '/link2'}" // sap.ui.core.URI 
					}
				)
			]
		});


		var app = new sap.m.App("myApp", {
			initialPage: "oPage"
		});
		app.addPage(oPage);
		return app;
	}
});
<br>

Here is the controller,

sap.ui.define([
	"sap/ui/core/mvc/Controller" , "sap/ui/model/json/JSONModel",
	"sap/ui/core/format/NumberFormat"
], function(Controller, JSONModel, NumberFormat) {
	"use strict";


	return Controller.extend("mysap.ui5.sandappSandApplication.controller.Main", {
	
		_data: {
			"number": ["9999.99"],
			"link1" : 'https://www.google.com',
			"link2" : 'https://www.aol.com'
		},


		onInit: function(evt) {
			var oModel = new JSONModel(this._data);
			this.getView().setModel(oModel);
			var oFmtSet =  NumberFormat.getCurrencyInstance();
			// var oLocale = oFmtSet.getCurrencyInstance();
		}


	});
});
<br>

Please find the screen shot below

Thanks, Pavan

meenakshi_raina
Advisor
Advisor
0 Kudos

This code is being called in my simple form. I guess it has something to do with it.

Not sure !

meenakshi_raina
Advisor
Advisor

I dont know why it did not work. So i used window.open(Link1);

this is working in my case. Thanks a lot 🙂

bpawanchand
Active Contributor
0 Kudos

thanks for providing the workaround

Answers (2)

Answers (2)

former_member316084
Discoverer

Hi,

If you are getting the link from an OData service, you could simply use URLHelper to redirect to a new page.

example:

sap.m.URLHelper.redirect(oData.results[0].Link,true);

Thanks,

Saira

Former Member
0 Kudos

Hi Meenakshi,

Can you try with 'Press' event in sap.m.Link..

https://openui5.hana.ondemand.com/#docs/api/symbols/sap.m.Link.html#event:press

I am not sure are you setting href value dynamically for the link.

Regards,

Mahi theja

meenakshi_raina
Advisor
Advisor
0 Kudos

This is because I don't have a fixed URL. It will vary depending on a document number.