PDA

View Full Version : How to add component to menu



marcb33
2 Jul 2015, 6:16 AM
Hello,

I am able to add a menu as a literal to a menu using add() but cannot add the same menu as a Menu.menu object to the menu.

1) add a menu as a literal:


G.hour24Menu = new Ext.menu.Menu({});
G.reportsMenu = new Ext.menu.Menu({});

G.reportsMenu.add({
text: "Etmaal grafieken",
hidden: false,
iconCls: "graph-icon",
menu: G.hour24Menu
});


But I cannot do this

2) add the same menu as a Menu.menu object:


G.hour24Menu = new Ext.menu.Menu({});
G.reportsMenu = new Ext.menu.Menu({});

G.reportsMenu.EtmaalGrafiekenMenu = new Ext.menu.Menu({
text: "Etmaal grafieken",
hidden: false,
iconCls: "graph-icon"
});
G.reportsMenu.add(G.reportsMenu.EtmaalGrafiekenMenu);


I prefer the 2nd method because it allows me to dynamically refer to menu G.reportsMenu.EtmaalGrafiekenMenu and add menu items to it (via G.reportsMenu.EtmaalGrafiekenMenu.add(menu)). Method 1) forces me to add a submenu, if it's empty, I have a ghost menu...

But my question is:
Why does method 2 not work?

Ext.getVersion() = version: "4.1.4.573"

Thanks,

Marc

skirtle
2 Jul 2015, 6:52 AM
It's Ext.menu.Item, not Ext.menu.Menu.

marcb33
3 Jul 2015, 12:22 AM
Thanks, that works. I created the menu as a variable because I want to add dynamically submenus to it. How do I do his? The code I have:



function addMenu24hourchart(menu) {
G.reportsMenu.EtmaalGrafiekenMenu.add(menu);
}
G.reportsMenu.EtmaalGrafiekenMenu = Ext.create("Ext.menu.Item",{
text: "Etmaal grafieken",
hidden: false,
iconCls: "graph-icon"
});
G.reportsMenu.add(G.reportsMenu.EtmaalGrafiekenMenu);


does not work - add() is not a function for Ext.menu.Item.

How can I add submenus to a menu.Item?

marcb33
3 Jul 2015, 1:35 AM
Ok, I found a way to have a menu item show when it has childItems and not show when it has no childMenuItems (it only functions as an 'anchor' here). For future reference here:



function addMenu24hourchart(menu) {
if(!G.reportsMenu.getComponent(G.reportsMenu.EtmaalGrafiekenMenu)){
G.reportsMenu.insert(0,G.reportsMenu.EtmaalGrafiekenMenu);
G.reportsMenu.EtmaalGrafiekenMenu.setMenu(G.hour24Menu)
}
G.hour24Menu.add(menu);
}


G.hour24Menu = new Ext.menu.Menu({
});


G.reportsMenu = new Ext.menu.Menu({
});


G.reportsMenu.EtmaalGrafiekenMenu = Ext.create("Ext.menu.Item",{
text: "Etmaal grafieken",
hidden: false,
iconCls: "graph-icon"
});


The menuItem is always created (G.reportsMenu.EtmaalGrafiekenMenu) but it is only added to the menu with the childItems when childItems are added to it in addMenu24hourchart(). If there is no subMenu the parent menu item is not shown.