Re: Adding attribute to selected menu item
Well there are a couple ways of doing this I recently dealt with this same problem. You can in fact use some Javascript on the onClick of your item (or onHover) to add to the affect, but either way you will want to have your default menu item selected from the get go when they are on that page (ie: they clicked on the blog section, that link in the menu should be highlighted now without clicking). So unless its all one page and your using ajax(I'll stop droning) The following should help: Link link = new Link(menuLink) { @Override protected void onComponentTag(ComponentTag tag) { if() tag.put(id,curent_menu); } } In Theory you can simply add the AttributeAppender from the get go depending on which page you are on, so when it reloads, it will be reloaded with a component. Now I take it you are using onclick to keep track of your page. Either you have to store a local variable that will record the page onClick, you have to have a method that each page overrides telling you the current page, or what I do is simply look at the PageParameters of my page. So as follows: params.getString(key); //the key depends on how you build your urls. This only works if every time a link is clicked it actually goes to another page with a different url. So as of now, in your onclick record the name of the menu clicked, then in the building section automaticlally add the appender This is what I mean (notice the unqouted changes): String selected = ; ListView lv = new ListView(mainMenu, menuList) { @Override protected void populateItem(final ListItem item) { Menu menuItem = (Menu) item.getModelObject(); Link link = new Link(menuLink) { @Override public void onClick() { selected = item.getPath(); System.out.println(item was clicked + item.getPath()); } }; if(item.getPath().equals(selected)) item.add(new AttributeAppender(id, new Model(current_menu), ;)); link.add(new Label(menuCaption, menuItem.getMenuNameDe())); item.add(link); } }; add(lv); Try the above. Notice the selected variable is now global and the if statement can be used to whatever will stay constant. I have never used getPath() before not sure what it returns, you might need the name of the menu which doesn't change etc no idea. Good Luck, Matthew Hello to all, i am trying to build a dynamic menu based on data from a database. So i thought i could build it like my code shows it. To highlight the selected menu i try to dynamically add an attribute to the li tag in the onClick method. I would also want to load and repaint other panels (for example loading sub menus ), when the user clicks a menu link. But it is not working. What am i doing wrong? -- View this message in context: http://apache-wicket.1842946.n4.nabble.com/Adding-attribute-to-selected-menu-item-tp3244327p3247201.html Sent from the Users forum mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Adding attribute to selected menu item
Hello to all, i am trying to build a dynamic menu based on data from a database. So i thought i could build it like my code shows it. To highlight the selected menu i try to dynamically add an attribute to the li tag in the onClick method. I would also want to load and repaint other panels (for example loading sub menus ), when the user clicks a menu link. But it is not working. What am i doing wrong? pre ListViewMenu lv = new ListViewMenu(mainMenu, menuList) { @Override protected void populateItem(final ListItemMenu item) { Menu menuItem = (Menu) item.getModelObject(); LinkMenu link = new LinkMenu(menuLink) { @Override public void onClick() { item.add(new AttributeAppender(id, new Model(current_menu), ;)); System.out.println(item was clicked + item.getPath()); } }; link.add(new Label(menuCaption, menuItem.getMenuNameDe())); item.add(link); } }; add(lv); /pre and this is my panel markup: pre body wicket:panel ul li wicket:id=mainMenu a href=# wicket:id=menuLinkspan wicket:id=menuCaption/span/a /li /ul /wicket:panel /body /pre My goal: pre body wicket:panel ul li wicket:id=mainMenu id=current_menu a href=# wicket:id=menuLinkspan wicket:id=menuCaption/span/a /li /ul /wicket:panel /body /pre Thank you for any help! - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org
Re: Adding attribute to selected menu item
Hi Haman, ListView items are recreated by default (see RepeatingView or ListView#setReuseItems). So your code is adding an AttributeAppender to an component that will be discarded in benefit of a new one for the next render. On Fri, Jan 28, 2011 at 10:28 AM, Haman Abel fha...@online.de wrote: Hello to all, i am trying to build a dynamic menu based on data from a database. So i thought i could build it like my code shows it. To highlight the selected menu i try to dynamically add an attribute to the li tag in the onClick method. I would also want to load and repaint other panels (for example loading sub menus ), when the user clicks a menu link. But it is not working. What am i doing wrong? pre ListViewMenu lv = new ListViewMenu(mainMenu, menuList) { @Override protected void populateItem(final ListItemMenu item) { Menu menuItem = (Menu) item.getModelObject(); LinkMenu link = new LinkMenu(menuLink) { @Override public void onClick() { item.add(new AttributeAppender(id, new Model(current_menu), ;)); System.out.println(item was clicked + item.getPath()); } }; link.add(new Label(menuCaption, menuItem.getMenuNameDe())); item.add(link); } }; add(lv); /pre and this is my panel markup: pre body wicket:panel ul li wicket:id=mainMenu a href=# wicket:id=menuLinkspan wicket:id=menuCaption/span/a /li /ul /wicket:panel /body /pre My goal: pre body wicket:panel ul li wicket:id=mainMenu id=current_menu a href=# wicket:id=menuLinkspan wicket:id=menuCaption/span/a /li /ul /wicket:panel /body /pre Thank you for any help! - To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org For additional commands, e-mail: users-h...@wicket.apache.org -- Pedro Henrique Oliveira dos Santos