ZK:关闭子窗口时刷新父窗口

时间:2016-06-09 05:55:28

标签: zk

我希望在ZK中实现在关闭子窗口时刷新父窗口。

Parent.zul - 这有一个下拉列表和一个按钮(标签为添加到下拉列表),为此下拉列表添加值。单击此按钮时,应打开子窗口,并选择在下拉列表中输入文本。

child.zul:这个文本框用于输入下拉列表的值和“关闭”按钮。单击关闭按钮时,它应关闭子窗口并刷新父窗口中的下拉列表。

提前感谢您的意见。

=============================================== ===========================

父母:sock.zul

<window id="sockWindow" title="New Sock" width="600px" apply="SockController" mode="modal" closable="true">
    <div align="left" style="float: left;" >    
        <button id="btnLookup" label="Manage Lookup" width="150px"/>
    </div>    
</window>

sockController.java: 公共类SockController扩展了SelectorComposer {

@Listen("onClick = button#btnLookup")
public void onClickAdd() throws Exception {
    showPopup(new EventListener<Event>() {
        @Override
        public void onEvent(final Event event) throws Exception {
            Object someData = event.getData(); // cast to whatever object you expect to give.   
            //Make your refresh code here.                
        }
    });          

}   

public static void showPopup(final EventListener<Event> eventListener)
    throws InterruptedException {
//you can give more params with the method to add them as arguments.
    Map arguments = new HashMap<String, String>();
    arguments.put("source", "parent.zul");
  //  arguments.put("hid", hwid.toString());
  //  arguments.put("displaymode", displaymode);

    openModal("/managelookup.zul",null, arguments, eventListener);
}

public static void openModal(final String page, final Component parent,
    final Map<String, Object> obMap,
    final EventListener<Event> onCloseListener)
    throws InterruptedException {
    for (final Map.Entry<String, Object> entry : obMap.entrySet()) {
        Executions.getCurrent().setAttribute(entry.getKey(),
            entry.getValue());
    }
    Executions.getCurrent().setAttribute(Composition.PARENT, null);
    final Component createComponents = Executions.createComponents(page,
            parent, obMap);
    Component parent1 = createComponents;
    parent1 = getWindow(parent1);
    if (parent1 instanceof Window) {
        final Window window = (Window) parent1;
        if (onCloseListener != null) {
            //attach the listener so when popup is closed the listener is called.
            window.addEventListener(Events.ON_CLOSE, onCloseListener);
            window.addEventListener(Events.ON_CANCEL, onCloseListener);
        }
        window.doModal();
    }
}  

}

child:managelookup.zul:     

        <button id="cancel" label="Close"/>
</window>

ManageLookupsController.java: public ManageLookupsController()抛出异常{

@Listen("onClick = button#cancel")
public void onClickCancel() {

    manageLookup.setVisible(false);
    manageLookup.detach();
    Events.postEvent(Events.ON_CLOSE, windowOrSpaceOwner, someData);
}    

}

2 个答案:

答案 0 :(得分:1)

我假设您的子窗口是另一个具有模态属性的窗口,因此:如果您使用的是MVVM,则可以使用GlobalCommand来刷新&#34;你的父窗口。

在您的父窗口中,viewModel会设置一个方法来刷新您的下拉列表,并将@GlobalCommand标记放入其中:

@GlobalCommand
public void refreshDropDown(){
    //Your code here
}

如果您有关闭窗口的方法,那么在您的子窗口中,然后将调用添加到该globalCommand:

public void closeWindow(){
    //your code to close the window here
    BindUtils.postGlobalCommand(null, null, "refreshDropDown", null);
}

或者直接在您的子窗口zul中,您可以调用它:

<button label="Close window" onClick="@command('close') @global-command('refresh')" />

您可以找到有关它的更多信息hereherehere

答案 1 :(得分:1)

我将在这里向您展示我们申请的一些样本:

在Parent composer中,我们打开弹出窗口并为回调提供eventlistener:

public void onActionShowPopup () {
    showPopup(new EventListener<Event>() {
            @Override
            public void onEvent(final Event event) throws Exception {
                Object someData = event.getData(); // cast to whatever object you expect to give.   
                //Make your refresh code here.                
            }
        });  
}

public static void showPopup(final EventListener<Event> eventListener)
        throws InterruptedException {
    //you can give more params with the method to add them as arguments.
    final Map<String, Object> args = new HashMap<>();
    args.put("modus", "modal");
    openModal("/WEB-INF/webpages/zk/popup/some_popup.zul",null, args, eventListener);
}

public static void openModal(final String page, final Component parent,
        final Map<String, Object> obMap,
        final EventListener<Event> onCloseListener)
        throws InterruptedException {
    for (final Map.Entry<String, Object> entry : obMap.entrySet()) {
        Executions.getCurrent().setAttribute(entry.getKey(),
                entry.getValue());
    }
    Executions.getCurrent().setAttribute(Composition.PARENT, null);
    final Component createComponents = Executions.createComponents(page,
            parent, obMap);
    Component parent1 = createComponents;
    parent1 = getWindow(parent1);
    if (parent1 instanceof Window) {
        final Window window = (Window) parent1;
        if (onCloseListener != null) {
            //attach the listener so when popup is closed the listener is called.
            window.addEventListener(Events.ON_CLOSE, onCloseListener);
            window.addEventListener(Events.ON_CANCEL, onCloseListener);
        }
        window.doModal();
    }
}

public Component getWindow (Component comp) {
    if (comp != null && !comp instanceof Window) {
        return getWindow(comp.getParent());
    }
    return comp;
}

当然,当你关闭时弹出窗口:

 Events.postEvent(Events.ON_CLOSE, windowOrSpaceOwner, someData);

我使用此代码创建了a fiddle以及它在哪里工作 也许你可以看到你犯错的地方?

希望这可能有所帮助。