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") { 
    protected void onComponentTag(ComponentTag tag) {

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

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());
>                        }
>                    };
                           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, 
> 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: 
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

Reply via email to