0

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

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

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

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

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

这是我的代码:

索引.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);
}

}

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

非常感谢和最好的问候。

4

2 回答 2

1

想到了两种方法。。

使用 ZK 绑定

使用ZK 的@Listen方法,您可以通过用分号 ( ;) 分隔事件来使用单个侦听器侦听多个事件。
例如:

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

当您注意到这些只是 CSS 选择器时,这可以被清除:

@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。

于 2013-05-23T15:09:40.607 回答
0

感谢 Sean Connoly 提供了正确的解决方案:

班上:

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);
}

}

索引.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>
于 2013-05-27T12:53:34.477 回答