/scripts/ahub.form.attachments.js
0

Dynamic Menu Button

Mar 11, 2017 at 12:29 PM

210

avatar image

Hi everbody,

I wanted to create a dynamic menu button. See code below:

var oMenu = new sap.m.MenuButton({
	"id":"testLink",
	"text":"testLink",
	"menu": new sap.m.Menu({
	"itemSelected": function(oEvent) {
		var oItem = oEvent.getParameter("item");
		oItem.firePress();}
	})
})

var menu = oMenu.getMenu();
var i = 0

while (i < size) {
	var name = testLinkModel.oData[i].TO.split("/")[1];
	menu.addItem(new sap.m.MenuItem({
		"text": name,
		"press": function() {oThis.getRouter().navTo(name);}
	}));
	i++;
}

But I have the problem that always the last name is used.

menu.addItem(new sap.m.MenuItem({
		"text": name,
		"press": function() {oThis.getRouter().navTo(name);}
	}));

For Example:

  1. Iteration: name=test1
  • addItem(text=test1, press=(oThis.getRouter().navTo(test1))
  1. Iteration: name=test2
  • addItem(text=test2, press=(oThis.getRouter().navTo(test2))

In the 2.Iteration he override the first item with:

addItem(text=test1, press=(oThis.getRouter().navTo(test2))

I hope you can understand what I mean.

Best regards

Yannick

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Jun Wu Mar 11, 2017 at 01:27 PM
1
var oMenu =newsap.m.MenuButton({"id":"testLink","text":"testLink","menu": new sap.m.Menu({"itemSelected": function(oEvent) {
		var oItem = oEvent.getParameter("item");
		oItem.firePress();}})})

var menu = oMenu.getMenu();
function  mypress(target){oThis.getRouter().navTo(target);}

function makepress(target){
return function(){
 mypress(target);


}
}

var i=0while(i<size){
	var name= testLinkModel.oData[i].TO.split("/")[1];
	menu.addItem(newsap.m.MenuItem({"text": name,"press":makepress(name) }));i++;}
Show 1 Share
10 |10000 characters needed characters left characters exceeded

Thank you! It works.

0
Skip to Content