-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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?
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
Asked: 2014-07-18 07:23:56 +0800
Seen: 97 times
Last updated: Jul 18 '14