cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Issue while bind menu items dynamically

muhsin_panakkal
Participant
2,198

Hai Experts,

I am trying to add Menu button through JS in dynamically, I have the below code.

onInit: function(evt) {
var that = this;
var bar = this.getView().byId("idbar1");
	bar.addContentLeft(new sap.m.Button({
					text: "Sample",
					press: function() {
						that.menuPress();
					}
				}));
 }

menuPress: function() {
			var oJSONModel = new sap.ui.model.json.JSONModel({
				"Plants": [{
					key: "0001",
					description: "Plant 0001"
				}, {
					key: "0002",
					description: "Plant 0002"
				}, {
					key: "0003",
					description: "Plant 0003"
				}]
			});
			var oItemTemplate = sap.ui.unified.MenuItem({
				text: "{description}"
			});
			var oMenu1 = new sap.ui.unified.Menu({
				items: {
					path: "/Plants",
					template: oItemTemplate
				}
			});
			oMenu1.setModel(oJSONModel);
			oMenu1.open();
		},

But I am getting an error while clicking the button.

can anybody guide me to resolve the issue.

Thanks,

Muhsin

Accepted Solutions (1)

Accepted Solutions (1)

junwu
SAP Champion
SAP Champion

how about adding sap.ui.unified.MenuItem to controller dependency?

muhsin_panakkal
Participant
0 Likes

Hai Jun Wu,

Can you please explain this..?

Thanks,

Muhsin

junwu
SAP Champion
SAP Champion
sap.ui.define(["sap/ui/core/mvc/Controller","sap/ui/unified/MenuItem"],function(Controller,MenuItem){"use strict";returnController.extend("",{});});
muhsin_panakkal
Participant
0 Likes

Dear Jun Wu,

I tried the above method, which resolved my issue. Thanks for your help.

Thanks,

Muhsin

Answers (2)

Answers (2)

sreehari_vpillai
Active Contributor
0 Likes

Adding to Florian's reply , this must be missing 'new" keyword . But, as you already tried that and facing the same error again ( which is highly unlikely ) , can you share the screenshot of the error you are facing after adding new statement ?

aslo , click on "View1.controller.js?eval;58" line from the error control , which takes you to the live running code in browser. See if "new" is reflecting there

Sree

muhsin_panakkal
Participant
0 Likes

Dear Sreehari,

I tried with new instance, I am getting the same error. Kindly find the below.

menuPress: function() {
			var oJSONModel = new sap.ui.model.json.JSONModel({
				"Plants": [{
key: "0001", description: "Plant 0001" }, { key: "0002", description: "Plant 0002" }, { key: "0003", description: "Plant 0003" }] }); var oItemTemplate = new sap.ui.unified.MenuItem({
				text: "{description}"
			});
			var oMenu1 = new sap.ui.unified.Menu({
				items: {
					path: "/Plants",
					template: oItemTemplate
				}
			});
oMenu1.setModel(oJSONModel); oMenu1.open(); },

and in the error, "View1.controller.js?eval;57"

Thanks,

Muhsin

sreehari_vpillai
Active Contributor

add these snippet in init , or just before you are using Menu and MenuItem

jQuery.sap.require("sap.ui.unified.Menu") ;

jQuery.sap.require("sap.ui.unified.MenuItem");

//your Menu code goes here 


pfefferf
Active Contributor
0 Likes

Think you missed the "new" keyword for creating an instance of sap.ui.unified.MenuItem

... 
var oItemTemplae = new sap.ui.unified.MenuItem( ...
muhsin_panakkal
Participant
0 Likes

Dear Florian,

Thanks for your response, I tried with above also, giving the same error.

Thanks,

Muhsin