-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi, can somebody help me with the problem of open a popup on a specific position?
I have a toolbarbutton. When it is clicked, a popup will open.
So I tried to use getTop() and getLeft() to get x and y for the toolbarbutton. Thus I could open the popup at the location I want by adding some offset pixels.
However, the getTop() and getLeft() methods both returns null. I do not understand it. Is it supposed to work?
Is there anyway I can use to open a popup on a specific location I want? I know I could use popup.open(x,y). However this method does not take percentage as parameter. I have to give it pixel value. In this way, when browser gets resized, the the popup position will screwed.
Can somebody help? Thank You!
top and left are attributes specified by user to adjust the position of toolbarbutton, e.g.
<toolbarbutton left="25px" label="button" id="btn" />
You can use position mechanism to position a popup, please refer to our demo:
You can also listen to onClientInfo event to reposition popup while browser window resize, and override js method to add some custom offset, please refer to the sample below:
test.zul
<zk xmlns:w="client">
<style>
.pos-class {
margin-left: 50px;
margin-top: 50px;
}
</style>
<window id="win" title="toolbar demo" border="normal" width="300px"
mode="overlapped" position="center"
apply="test.TestComposer">
<toolbar>
<toolbarbutton left="25px" label="button" id="btn" />
</toolbar>
<popup id="pp">
<!-- mix position with custom offset override open and afterOpenAnima_ -->
<attribute w:name="open"><![CDATA[
function (ref, offset, position, opts) {
jq(this.$n()).removeClass('pos-class');
this.$open(ref, offset, position, opts);
}
]]></attribute>
<attribute w:name="afterOpenAnima_"><![CDATA[
function (ref, offset, position, opts) {
this.$afterOpenAnima_(ref, offset, position, opts);
var wgt = this;
setTimeout(function () {
zk.log('after open');
jq(wgt.$n()).addClass('pos-class');
}, 50);
}
]]></attribute>
content
</popup>
</window>
</zk>
TestComposer.java
package test;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.OpenEvent;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Popup;
import org.zkoss.zul.Toolbarbutton;
public class TestComposer extends SelectorComposer {
@Wire
Toolbarbutton btn;
@Wire
Popup pp;
boolean _open;
@Listen ("onClick = #btn")
public void openPopup (Event event) {
_open = true;
pp.open(btn, "end_before");
}
// update Popup position by listening onClientInfo and reopen Popup
@Listen ("onClientInfo = #win")
public void reopenPopup (Event event) {
if (_open) {
pp.close();
pp.open(btn, "end_before");
System.out.println("onClientInfo");
}
}
// keep open status by listen to onOpen event of Popup
@Listen ("onOpen = #pp")
public void onPopupOpen (OpenEvent event) {
_open = event.isOpen();
}
}
Asked: 2014-02-13 21:45:33 +0800
Seen: 107 times
Last updated: Feb 14 '14