1

Strange behaviour of composite component and menupopup

asked 2016-01-23 13:10:42 +0800

Bhushanngage gravatar image Bhushanngage
59 6

I have one composite component. Its zul is cc.zul

<zk>
<div context="editPopup">
    <label id="lbl" />
</div>
<menupopup id="editPopup">
    <menuitem label="Message" />
    <menuitem label="Profile" />
</menupopup>
</zk>

Its corresponding listener is CompositeComp.java

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.MouseEvent;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Div;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;

public class CompositeComp extends Div implements IdSpace {

@Wire
Label lbl;

// Default Constructor.
public CompositeComp(int id) {
    Executions.createComponents("cc.zul", this, null);
    Selectors.wireComponents(this, this, false);
    Selectors.wireEventListeners(this, this);
    this.id = id;
    lbl.setValue("Hello " + id);
}

public int getCompId() {
    return id;
}
    //Here is magic happening
@Listen("onClick = menuitem")
public void onSelectMenu(MouseEvent event) {
    Messagebox.show(String.valueOf(id));
}
}

I want to show above composite component in my container.zul

<window id="win" apply="the.bhushan.zk.listeners.Container">
    <div id="container" />
</window>

Listener Container.java is as follow

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Div;

public class Container extends SelectorComposer<Component> {

    @Wire
    Div container;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        for (int i = 0; i < 5; i++) {
            container.appendChild(new CompositeComp(i));
        }
    }
}

In listener Container.java, I am appending Composite component 5 times. When I right click on any component in screen, and after clicking any menu Messagebox shows value "4" always. Please help.

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-01-24 20:03:27 +0800

chillworld gravatar image chillworld flag of Belgium
5367 4 9
https://github.com/chillw...

If you check on what class is triggered, you will find out that it's always the same menupopup what's behind it.

Here is a fiddle what resolves your issue. I'm just giving a unique id to the menupopup so the reference can be set correct.

Chill.

link publish delete flag offensive edit

Comments

@chillworld - Great, it worked.

Bhushanngage ( 2016-01-27 05:03:52 +0800 )edit

@chillworld - Though, it worked, I still didn't get what was the issue :(

Bhushanngage ( 2016-01-28 06:03:34 +0800 )edit

mine guess is that the menupopup ID is going through the IdSpace witch resolves in taking the last one created for all popups.

chillworld ( 2016-01-28 06:25:30 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2016-01-23 13:10:42 +0800

Seen: 18 times

Last updated: Jan 24 '16

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More