0

Stop close modal window

asked 2014-07-18 07:23:56 +0800

sentinal gravatar image sentinal
11 2

updated 2014-07-18 07:24:16 +0800

I use viewModel in my app and i have closable window. When i click on close button in top-right of window command caused :

onClose="@command('close',win=mywindow)"

@Command
public void close(@BindingParam("win")Window win){

MessageBoxq.show("Are you sure want to close window?",....);
}

In method i have meesage box where i ask about close window. But when i click OK or CANCEL window close anyway. How to stop close window if i click CANCEL?

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-07-18 09:39:58 +0800

cyiannoulis gravatar image cyiannoulis
1191 10

All you have to do is to stop propagation of the "close" event and then ask for a user confirmation using an event listener. Here is a complete example:

A simple main page:

<?page title="Main Page" contentType="text/html;charset=UTF-8"?>
<zk>
<window title="Main Page" border="normal" 
        apply="org.zkoss.bind.BindComposer" 
        viewModel="@id('vm') @init('MainPageVM')">

    <button label="Open Modal Window" onClick="@command('open-modal-window')" />

</window>
</zk>

and a view model:

public class MainPageVM {

    @Command("open-modal-window")
    public void openModalWindow() {
        Executions.createComponents("modal-window.zul", null, null);        
    }

}

And here is the modal dialog:

<?page title="Modal Window" contentType="text/html;charset=UTF-8"?>
<zk>
<window id="winDialog"
        title="Modal Window" border="normal"
        mode="modal"
        width="400px" height="400px"
        closable="true"
        onClose="@command('close')"     
        apply="org.zkoss.bind.BindComposer" 
        viewModel="@id('vm') @init('ModalWindowVM')">

    <separator height="50px"></separator>
    <div align="center">
        Press the close button to return
    </div>

</window>
</zk>

And the view model:

public class ModalWindowVM {

  @Command("close")
  public void onClose(@SelectorParam("#winDialog") final Component window, 
              @ContextParam(ContextType.TRIGGER_EVENT) Event e) 
  {
     e.stopPropagation();
     Messagebox.show("Are you sure you want to close this window?", "Cancel", 
              new Messagebox.Button[]{Messagebox.Button.YES, Messagebox.Button.NO }, 
              Messagebox.QUESTION, 
              new EventListener<Messagebox.ClickEvent>() {
            @Override
            public void onEvent(ClickEvent event) throws Exception {
              if (Messagebox.Button.YES.equals( event.getButton() ))
                  window.detach();
            }
              });
     }
}

Hope that helps

Costas

link publish delete flag offensive 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: 2014-07-18 07:23:56 +0800

Seen: 79 times

Last updated: Jul 18 '14

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