如何使用 Wicket 的 AjaxLink 创建可更新面板?

发布于 2024-08-26 09:03:01 字数 295 浏览 7 评论 0原文

我尝试实现一个使用 Wicket 的 AjaxLink 更新网站中表格的链接(实际上是很多链接)。但我失败了,表永远不会更新(我有“setOutputMarkupId(true)”并调用“setDefaultModelObject”和“addComponent”,但一定还有其他错误)。

如何实现一个包含多个链接的面板和一个根据单击的链接显示动态数据的表格?有人可以举个例子吗(也许有两个链接,当单击第一个链接时,表格显示 1-10 之间的两个随机数字,当单击第二个链接时,表格显示 1-100 之间的随机数字)?不重新加载整个页面,而只加载表格的 html?

I try to realize a link (in fact many links) that update a table in an website using an AjaxLink of Wicket. But I fail, the table is never updated (I have "setOutputMarkupId(true)" and call "setDefaultModelObject" and "addComponent", but there must be something other thats wrong).

How can I realize a panel with a number of links and a table that displays dynamic data, dependent on the link clicked? Can someone give an example (Maybe two links, when the first one is clicked the table displays two random numbers from 1-10, when the second is clicked the table displays random numbers from 1-100)? Without reloading the entire page, but only the html for the table?

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

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

发布评论

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

评论(4

霓裳挽歌倾城醉 2024-09-02 09:03:01

一个可能的原因可能是您没有使用“可刷新”模型,而是获取列表项并将它们直接传递给组件,因此列表在会话中序列化并且不会更新。

如果是这种情况,请将 LoadableDetachableModel(在其加载方法中检索列表)传递给组件。如果没有看到您的代码,我无法更具体。

One possible reason might be that you are not using a 'refreshable' model, but rather fetching the list items and passing them directly to the component, thus the list get's serialized in the session and doesn't get updated.

If this is the case, pass a LoadableDetachableModel (that retrieves the list in it's load method) to the component. I can't be more specific without seeing your code.

甲如呢乙后呢 2024-09-02 09:03:01

我认为您没有非常清楚地定义自己在做什么。

这两个表有不同的实现吗?如果是这样,那么您的代码是正确的 - 您必须用新组件替换旧组件,然后将新组件添加到 ajax 响应中。

但实际上,我想象您有 1 个表组件实现。

因此,您需要做的是这样的:(

public class RandomNumberListModel extends LoadableDetachableModel {
    private int upperBound;

    public RandomNumberListModel(int upperBound) {...}

    public void setUpperBound(int upperBound) {...}

    protected Object load() {
        // generate random number list using upper bound
        // return list
    }        
}

...

final MyTableComponent table = new MyTableComponent(new RandomNumberListModel(30));
add(table);    
AjaxLink link = new AjaxLink("myButton") {
    public void onClick(final AjaxRequestTarget target) {
        table.getModel().setUpperBound(100);
        target.addComponent(table);
    }
};
add(link);

编辑)我添加了一个动态的、可重用的模型来说明该模型如何工作。有不同的方法可以实现这一点,具体取决于您想要重用的内容。关键点是模型动态生成列表,即根据请求生成列表,并且可以在 onClick 回调中操作数字范围的 upperBound。

I don't think you've defined what you are doing very clearly.

Are these 2 tables different implementations? If so, then your code is correct - you have to replace the old component with the new one, then add the new one to the ajax response.

Realistically though, I'd imagine that you have 1 table component implementation.

What you therefore need to do, is something like this:

public class RandomNumberListModel extends LoadableDetachableModel {
    private int upperBound;

    public RandomNumberListModel(int upperBound) {...}

    public void setUpperBound(int upperBound) {...}

    protected Object load() {
        // generate random number list using upper bound
        // return list
    }        
}

...

final MyTableComponent table = new MyTableComponent(new RandomNumberListModel(30));
add(table);    
AjaxLink link = new AjaxLink("myButton") {
    public void onClick(final AjaxRequestTarget target) {
        table.getModel().setUpperBound(100);
        target.addComponent(table);
    }
};
add(link);

(Edit) I've added a dynamic, reusable model to illustrate how the model would work. There are different ways of implementing this, depending on what you want to be reusable. The key point is that the model generates the list dynamically, i.e. per request, and the upperBound of the number range can be manipulated in the onClick callback.

烟火散人牵绊 2024-09-02 09:03:01

就像 jboyd 所问的那样,您是否有知道在 Ajax 响应中发送回表内容的代码?:

final Component tableComponent = ....;
AjaxLink link = new AjaxLink("myButton"){
    public void onClick(final AjaxRequestTarget target) {
        target.addComponent(tableComponent);
    }
};
add(link);

addComponent 部分是 jboyd 所指的部分。

Like jboyd was asking do you have code that knows to send the table contents back in the Ajax response?:

final Component tableComponent = ....;
AjaxLink link = new AjaxLink("myButton"){
    public void onClick(final AjaxRequestTarget target) {
        target.addComponent(tableComponent);
    }
};
add(link);

The addComponent piece is the part jboyd is referring to.

放手` 2024-09-02 09:03:01

wicketstuff.org 示例中有一个这样的示例,即 tree/tree 表 之一。顶部的三个链接更改了表格。

There is one such example among the wicketstuff.org examples, the tree/tree table one. The three links at the top change the table.

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