ZK:如何拦截/使用来自父菜单栏的事件?

时间:2013-05-23 13:48:01

标签: java ajax menu listener zk

我刚刚开始学习ZK,而且我遇到了一个严重问题,即从包含多个菜单和menuitem的父菜单栏中拦截一个事件(onClick)。

我正在使用AJAX来更改内部面板的src uri。

我不想为每个菜单项创建一个唯一的类。

我想(但我不知道是否以及如何可能)创建一个类来拦截来自菜单栏的onClick事件,但事实上,当这样的事件出现在菜单项上时。

所以我想将类控制器绑定到菜单栏,但同时获取用户点击的菜单项。

这是我的代码:

Index.zul

<zk>
<style>.z-menubar-hor, .z-menubar-ver { border: 1px solid #D8D8D8; }
.content {overflow-x: hidden;overflow-y: scroll;}</style>

        <borderlayout hflex="1" vflex="1" id="mainBorder">
            <north>
                <include src="topBanner.zul"/>
            </north>
            <west>
                <panel hflex="1" vflex="1" border="normal" title="MENU COMANDI" apply="mainPage.MenuAjaxbasedController">
                    <panelchildren style="padding:5px;">
                        <menubar id="menubar" orient="vertical" autodrop="true">
                            <menu label="ANAGRAFICA" id="anagrafica">
                                <menupopup>
                                    <menuitem id="contatti" label="CONTATTI" />
                                    <menuitem id="listeDiDistribuzione" label="LISTE DISTRIBUZIONE" />
                                    <menuitem id="sottoscrizioni" label="SOTTOSCRIZIONI" />
                                    <menuitem id="profilo" label="PROFILO" />
                                </menupopup>
                            </menu>
                            <menu label="MESSAGGI">
                                <menupopup>
                                    <menuitem id="nuovaNewsletter" label="NUOVA NEWSLETTER" />
                                    <menuitem id="messaggiInviati" label="MESSAGGI INVIATI" />
                                    <menuitem id="bozze" label="BOZZE" />
                                </menupopup>
                            </menu>
                            <menu label="STRUMENTI">
                                <menupopup>
                                    <menuitem id="smtpServers" label="SMTP SERVERS" />
                                </menupopup>
                            </menu>
                        </menubar>
                    </panelchildren>
                </panel>
            </west>
            <center id="mainContent" autoscroll="true" border="none">
                <include id="mainContentInclude" self="@define(content)" src=""/>
            </center>
            <south>
                <include src="bottomFooter.zul"/>
            </south>
        </borderlayout>

MenuAjaxBasedController.java

public class MenuAjaxbasedController extends SelectorComposer<Component>{

private static final long serialVersionUID = 1L;

@Wire
Center mainContent;

@Wire
Borderlayout mainBorder;

@Wire
Menuitem contatti;

@Wire
Include mainContentInclude;

@Wire
Menubar menubar;

@Listen("onClick = #contatti")
public void change_page(Event e){
    // Menubar mb = (Menubar) e.getTarget();
    System.out.println(e.getTarget());
    String locationUri = "anagraficaContattiMainGrid.zul";
    // alert("menu link test");
    Include include = (Include)Selectors.iterable(mainBorder.getPage(), "#mainContentInclude").iterator().next();
    include.setSrc(locationUri);
}

}

如您所见,我现在只收听一个菜单项。

非常感谢和最诚挚的问候。

2 个答案:

答案 0 :(得分:1)

有两种想法......

使用ZK绑定

使用ZK's @Listen approach,您可以使用分号(;)分隔事件,从而通过单个侦听器侦听多个事件。
例如:

@Listen("onClick = #contatti; onClick = #listeDiDistribuzione; onClick = #sottoscrizioni")
public void changePage(Event event) {
    event.getTarget(); // the specific button clicked
}

当您注意到these are just CSS selectors

时,可以清除此内容
@Listen("onClick = #menubar ~ menuitem")
public void changePage(Event event) {
    event.getTarget(); // the specific button clicked
}

这里有许多可能性,阅读文档(上面链接),你会得到一些好的东西。

摇摆风格

另一种解决方法是从Java端进行此操作。这是非常Swing-ish而不是在视图之间干净地分开,但完全有效且值得一提。

private static final EventListener<Event> LISTENER = new EventListener<Event>() {
    @Override
    public void onEvent(Event event) {
        event.getTarget(); // the specific button clicked
    }
}

@Wire
private Menuitem contatti;
@Wire
private Menuitem listeDiDistribuzione;

@Override
public void doAfterCompose(Component component) {
    contatti.addEventListener(Events.ON_CLICK, LISTENER);
    listeDiDistribuzione.addEventListener(Events.ON_CLICK, LISTENER);
}

当然,在这里你可以通过一些CSS选择简化事情..

@Wire("#menubar ~ menuitem")
private List<Menuitem> menuitems;

@Override
public void doAfterCompose(Component component) {
    for (Menuitem menuitem : menuitems) {
        menuitem.addEventListener(Events.ON_CLICK, LISTENER);
    }
}

StackOverflow中的Benvenuto。

答案 1 :(得分:0)

感谢Sean Connolly在这里找到了正确的解决方案:

班级:

package mainPage;


public class MenuAjaxbasedController extends SelectorComposer<Component>{

private static final long serialVersionUID = 1L;

@Wire
Center mainContent;

@Wire
Borderlayout mainBorder;

@Wire
Menuitem contatti;

@Wire
Include mainContentInclude;

@Wire
Menubar menubar1;

@Listen("onClick = #menubar1 > menu > menupopup > menuitem")
// @Listen("onClick = #contatti; onClick = #listeDiDistribuzione; onClick = #sottoscrizioni; onClick = #profilo; onClick = #nuovaNewsletter; onClick = #messaggiInviati; onClick = #bozze; onClick = #smtpServers;")
public void change_page(Event e){

    System.out.println(e.getTarget());
    System.out.println(e.getTarget().getId());

    String menuItemId = e.getTarget().getId();
    String locationUri = "";

    switch (menuItemId) {
    case "contatti":  locationUri = "anagraficaContattiMainGrid.zul";
             break;
    case "listeDiDistribuzione":  locationUri = "";
             break;
    case "sottoscrizioni":  locationUri = "";
             break;
    case "profilo":  locationUri = "";
             break;
    case "nuovaNewsletter":  locationUri = "";
             break;
    case "messaggiInviati":  locationUri = "";
             break;
    case "bozze":  locationUri = "";
            break;
    case "smtpServers": locationUri = "";
             break;
    default: locationUri = "anagraficaContattiMainGrid.zul";
             break;
    }

    System.out.println(locationUri);

    Include include = (Include)Selectors.iterable(mainBorder.getPage(), "#mainContentInclude").iterator().next();
    include.setSrc(locationUri);
}

}

index.zul

<zk>
<style>.z-menubar-hor, .z-menubar-ver { border: 1px solid #D8D8D8; }
.content {overflow-x: hidden;overflow-y: scroll;}</style>

        <borderlayout hflex="1" vflex="1" id="mainBorder" if="${not sessionScope.userCredential.anonymous}">
            <north>
                <include src="topBanner.zul"/>
            </north>
            <west>
                <panel hflex="1" vflex="1" border="normal" title="MENU COMANDI" apply="mainPage.MenuAjaxbasedController">
                    <panelchildren style="padding:5px;">
                        <menubar id="menubar1" orient="vertical" autodrop="true">
                            <menu label="ANAGRAFICA" id="anagrafica">
                                <menupopup>
                                    <menuitem id="contatti" label="CONTATTI" />
                                    <menuitem id="listeDiDistribuzione" label="LISTE DISTRIBUZIONE" />
                                    <menuitem id="sottoscrizioni" label="SOTTOSCRIZIONI" />
                                    <menuitem id="profilo" label="PROFILO" />
                                </menupopup>
                            </menu>
                            <menu label="MESSAGGI">
                                <menupopup>
                                    <menuitem id="nuovaNewsletter" label="NUOVA NEWSLETTER" />
                                    <menuitem id="messaggiInviati" label="MESSAGGI INVIATI" />
                                    <menuitem id="bozze" label="BOZZE" />
                                </menupopup>
                            </menu>
                            <menu label="STRUMENTI">
                                <menupopup>
                                    <menuitem id="smtpServers" label="SMTP SERVERS" />
                                </menupopup>
                            </menu>
                        </menubar>
                    </panelchildren>
                </panel>
            </west>
            <center id="mainContent" autoscroll="true" border="none">
                <include id="mainContentInclude" self="@define(content)" src=""/>
            </center>
            <south>
                <include src="bottomFooter.zul"/>
            </south>
        </borderlayout>
            <div if="${sessionScope.userCredential.anonymous}">
            </div>
</zk>