avatar image
Former Member

Dynamic Menu Button

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Mar 11, 2017 at 01:27 PM
    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++;}
    Add comment
    10|10000 characters needed characters exceeded

Skip to Content