如何用我的GWT小部件替换元素? [英] How to replace element with my GWT widget?

查看:124
本文介绍了如何用我的GWT小部件替换元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用我的小部件组件来替换已知的html元素? (强调单词'替换',我不想将部件放入该元素中。:)

 <身体GT; 
< img />
< div />
< a id =tmpEl/>
...
< img />
< / body>

会变成

 <身体GT; 
< img />
< div />
< div class =gwt-panel> ...< / div>
...
< img />
< / body>

我试过这样的...

  tmpEl.getParentElement()。replaceChild(myPanel.getElement(),tmpEl); 

...但生成的DOM元素是'聋子',即他们没有收到点击事件。 (为了实现这个功能,我可能需要调用RootPanel.get()。采用(widget),但该方法不可访问。)



我想, HTMLPanel.addAndReplaceElement 可能是答案,但只有这样当您的'占位符'元素是HTMLPanel小部件的(直接)子元素时工作。这显然不是我的情况。 :(b / b)

请注意,我只知道那个元素的id,我没有创建它,简单地说:我需要对于'更高级别的DOM操作':我会很高兴地操纵最高级别的DOM,如果它允许我放置小部件而不是该占位符元素。

解决方案

看起来,在插入widget到DOM后调用 widget.onAttach()

  class MyWidget继承了Composite 
{
...

public void附加()
{
/ * Widget.onAttach()受保护
* /
onAttach();

/ *对于所有小部件父窗口小部件
* /
RootPanel.detachOnWindowClose(this);
}
}

tmpEl.getParentElement()。replaceChild(myWidget.getElement() ,tmpEl);
myWidget.attach();

Google Web Toolkit小组



我仍然想知道为什么没有 RootPanel.addAndReplaceElement(Widget,Element),类似于 HTMLPanel.addAndReplaceElement(Widget,Element)


Is it possible to replace known html element with my widget component? (Emphasis on the word 'replace', I don't want to put the widget in that element. :)

<body>
  <img />
  <div />
  <a id="tmpEl" />
  ...
  <img />
</body>

would become

<body>
  <img />
  <div />
  <div class="gwt-panel">...</div>
  ...
  <img />
</body>

I tried something like this...

tmpEl.getParentElement().replaceChild(myPanel.getElement(), tmpEl);

...but the resulting DOM elements were 'deaf', i.e. they did not receive click events. (To make this work I would probably have to call RootPanel.get().adopt(widget), but that method is not accessible.)

For a second I thought HTMLPanel.addAndReplaceElement could be the answer, but that only works when your 'placeholder' element is (direct) child of HTMLPanel widget. Which is obviously not my case. :(

Note please that I only know id of that element, I'm not creating it. Simply put: I need exactly what the question says.

As for 'DOM manipulation at higher level': I will happily manipulate the DOM at highest possible level if it lets me place widget instead of that placeholder element.

解决方案

It seems that calling widget.onAttach() after inserting widget into DOM does the trick.

class MyWidget extends Composite
{
  ...

  public void attach()
  {
    /* Widget.onAttach() is protected
     */
    onAttach();

    /* mandatory for all widgets without parent widget
     */
    RootPanel.detachOnWindowClose(this);
  }
}

tmpEl.getParentElement().replaceChild(myWidget.getElement(), tmpEl);
myWidget.attach();

Credit goes to André at Google Web Toolkit group.

I still wonder though why there is no RootPanel.addAndReplaceElement(Widget, Element), similar to HTMLPanel.addAndReplaceElement(Widget, Element).

这篇关于如何用我的GWT小部件替换元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆