Apache wicket:来自 Linktree.onNodeSelected() 的 Componenet.replaceWith(component)

发布于 2024-11-25 09:56:00 字数 497 浏览 1 评论 0原文

我有一个用于导航数据的链接树,它将替换另一个 div 中的组件。

为此,在树中我有 treestatelistener 实现

public void nodeSelected(Object node) {
    log.debug("nodeSelected: " + node+ " class: " + node.getClass());
            Component content = new Label("content");
            content.replaceWith(new Label("content", "Hello World"));
        }
    }
}

我得到的只是消息:

“此方法只能在已经被调用的组件上调用 添加到其父级。”

ID 为“content”的标签在 init 时添加到页面中,因此它之前就存在。我做错了什么?

I have a linktree for navigation over data which shall replace a component in another div.

For this in the tree I have the treestatelistener implementing

public void nodeSelected(Object node) {
    log.debug("nodeSelected: " + node+ " class: " + node.getClass());
            Component content = new Label("content");
            content.replaceWith(new Label("content", "Hello World"));
        }
    }
}

All I get is the message:

"This method can only be called on a component that has already been
added to its parent."

The Label with Id "content" is added to the page on init, so it is there before. What do I do wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2024-12-02 09:56:00

每次调用树节点侦听器时,您都会创建一个新的父组件,而不添加“内容”标签。
我建议尝试这样的事情 -

public MyPage extends WebPage {
  private Component parent = new WebMarkupContainer("parent");

  public WebPage() {
    parent.add(new Label("content", "hi");
  }
  ...
  public void nodeSelected(Object node) {
    log.debug("nodeSelected: " + node+ " class: " + node.getClass());
        parent.replaceWith(new Label("content", "Hello World"));
    }
  }
}

You are creating a new parent Component every time your treenode listener is called, without adding the 'content' label.
What I would suggest is to try something like this -

public MyPage extends WebPage {
  private Component parent = new WebMarkupContainer("parent");

  public WebPage() {
    parent.add(new Label("content", "hi");
  }
  ...
  public void nodeSelected(Object node) {
    log.debug("nodeSelected: " + node+ " class: " + node.getClass());
        parent.replaceWith(new Label("content", "Hello World"));
    }
  }
}
衣神在巴黎 2024-12-02 09:56:00

我总是在调用 Component.replaceWith() 时遇到问题。由于我使用 AJAX 执行此操作,因此我始终需要调用 target.add()。

我的解决方案现在是让类 MyTree 扩展 LinkTree 并在该类中重写 newNodeComponent()。

由于在我的应用程序中,一切都发生在 IndexPage.class 上,并且我只是替换组件,因此我向 IndexPage.Class 添加了一个方法 handleSelection()(extends BasePage 扩展网页)。在那里,我根据单击的节点对象决定如何替换 IndexPage 中的组件

Public Class MyTree extends Linktree{
  protected Component newNodeComponent(String id, IModel<Object> model) {
  @Override
        protected void onNodeLinkClicked(Object node, BaseTree tree, AjaxRequestTarget target) {
            super.onNodeLinkClicked(node, tree, target);
            ((IndexPage)getPage()).handleSelection(target, (TreeNode)node);
        }

        @Override
        protected ResourceReference getResourceFolderOpen(Object node) {
            return getResourceForNode(node);
        }

        @Override
        protected ResourceReference getResourceFolderClosed(Object node) {
            return getResourceForNode(node);
        }

        @Override
        protected ResourceReference getResourceItemLeaf(Object node) {
            return super.getResourceItemLeaf(node);
        }

        @Override
        protected ResourceReference getImageResourceReference(BaseTree tree, Object node) {
            return getResourceForNode(node);
        }
  } 
}

我希望对此进行正确解释,否则:需要问题或评论!

I always have issues with just calling Component.replaceWith(). Since I am doing this with AJAX, I always need to call a target.add().

My solution is for now to have a class MyTree extend LinkTree and in that class override the newNodeComponent().

Since in my application everything happens on the IndexPage.class and there I just replace components, I added a method handleSelection() to the IndexPage.Class(extends BasePage extends webpage). There I decide, based on the clicked nodeObject, how to replace a component in the IndexPage

Public Class MyTree extends Linktree{
  protected Component newNodeComponent(String id, IModel<Object> model) {
  @Override
        protected void onNodeLinkClicked(Object node, BaseTree tree, AjaxRequestTarget target) {
            super.onNodeLinkClicked(node, tree, target);
            ((IndexPage)getPage()).handleSelection(target, (TreeNode)node);
        }

        @Override
        protected ResourceReference getResourceFolderOpen(Object node) {
            return getResourceForNode(node);
        }

        @Override
        protected ResourceReference getResourceFolderClosed(Object node) {
            return getResourceForNode(node);
        }

        @Override
        protected ResourceReference getResourceItemLeaf(Object node) {
            return super.getResourceItemLeaf(node);
        }

        @Override
        protected ResourceReference getImageResourceReference(BaseTree tree, Object node) {
            return getResourceForNode(node);
        }
  } 
}

I hope this is explained propperly, else: questions or comments wanted!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文