获取 GWT DialogBox 绝对位置 - onLoad/onAttach/show 没有帮助
我陷入了获取 DialogBox 的绝对位置的困境。我知道这是 PopupPanel
(它是 DialogBox
的父级)设置 的常见问题(也是奇怪的解决方法),但是如果我怎么办?想要得到它,盒子附加到 DOM 的确切时刻是什么?重写 show
、 onAttach
和 show
都没有帮助:
class MyDialog extends DialogBox {
public MyDialog(. . .) {
ComplexPanel vert = new VerticalPanel();
vert.add("Test");
vert.add(new Button("Close", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
MyDialog.this.hide();
}
}));
setWidget(vert);
this.addAttachHandler(new AttachEvent.Handler() {
@Override
public void onAttachOrDetach(AttachEvent event) {
if (event.isAttached()) Log.debug("attach:"+MyDialog.this.getAbsoluteLeft() +";"+
MyDialog.this.getAbsoluteTop());
}
});
}
@Override
protected void onLoad() {
super.onLoad();
Log.debug("load:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}
@Override
public void show() {
super.show();
Log.debug("show:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}
}
所以当我调用 new MyDialog().show();,所有这些行都会记录
0;0
,但是对话框位于页面的中心。但我想要的是 offsetParent 位置链的总和。 (如果使用 JSNI 来检查的话,即使在 JavaScript 中,这些时刻它们也是 0)
同样,setPopupPositionAndShow
允许设置位置,但不能获取它:(
I am stuck in getting an absolute position of DialogBox
. I know it is the common problem (and strange workaround) for PopupPanel
(which is parent to DialogBox
) to set it, but what if I want to get it, what is the exact moment when the box attached to DOM? Neither overriding show
nor onAttach
nor show
does not help:
class MyDialog extends DialogBox {
public MyDialog(. . .) {
ComplexPanel vert = new VerticalPanel();
vert.add("Test");
vert.add(new Button("Close", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
MyDialog.this.hide();
}
}));
setWidget(vert);
this.addAttachHandler(new AttachEvent.Handler() {
@Override
public void onAttachOrDetach(AttachEvent event) {
if (event.isAttached()) Log.debug("attach:"+MyDialog.this.getAbsoluteLeft() +";"+
MyDialog.this.getAbsoluteTop());
}
});
}
@Override
protected void onLoad() {
super.onLoad();
Log.debug("load:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}
@Override
public void show() {
super.show();
Log.debug("show:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}
}
So when I call new MyDialog().show();
, all this lines do log 0;0
, however dialog is positioned in center of a page. But what I want is the sum of the chain of offsetParent
positions. (And they are 0 in these moments even in JavaScript, if use JSNI to check this)
Again, setPopupPositionAndShow
allows to set position but not get it :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我已经让它工作了:
它获得了正确的位置,我希望这是正确的方法,并且每次都会调用
setPopupPosition
。您甚至可以在使用setPopupPositionAndShow
时手动调用它。我认为让这个问题留在“为了子孙后代”是明智的。
更新。如果您计划调用
center(...)
或对话框的某些类似方法,请注意setPopupPosition
将被调用两次或更多次(可能是第一次使用0, 0
),即使您会检查它是否isAttached()
。添加一些额外的检查以确保当前调用中的位置正确。Finally, I've got this to work:
It gets the proper position and I hope it is the right way to do it and
setPopupPosition
is called every time. You will even call it manually when usingsetPopupPositionAndShow
.I think it will be wise to let this question stay at SO "for future generations".
Upd. If you plan to call
center(...)
or some similar method of your dialog, be aware thatsetPopupPosition
will be called twice or more times (may be first time with0, 0
), even if you'll check if itisAttached()
. Add some additional check to ensure that positions are correct in current call.