Wicket 面板需要刷新页面才能使 javascript 脚本正常工作

发布于 2024-10-29 05:18:26 字数 1056 浏览 3 评论 0原文

我正在创建一个带有多个选项卡的网页。为了实现这一点,我使用了 wicket AjaxTabbedPanel 和几个 AbstractTab。在每个选项卡中,我都有包含数据的表格,并且我使用 JavaScript 脚本来使表格可排序。

public TabbedPage() {
    List<ITab> tabs = new ArrayList<ITab>();
    tabs.add(new AbstractTab(new Model<String>("first tab")) {
        public Panel getPanel(String panelId) {
            return new TablePanel(panelId);
        }
    });

    tabs.add(new AbstractTab(new Model<String>("second tab")) {
        public Panel getPanel(String panelId) {
            return new TablePanel(panelId);
        }
    });

    add(new AjaxTabbedPanel("tabs", tabs));
}

当我加载页面时,默认选择的选项卡中的表格是可排序的。但是,一旦我单击任何链接跳转到其他选项卡(包括已选择的选项卡),任何选项卡中的任何表格都不允许我对它们进行排序(包括之前正在使用的表格 -默认选项卡中的表)。如果我刷新页面,我可以对表格(刷新时选择的选项卡)进行排序,但是一旦我单击任何链接来切换选项卡,表格就会再次停止具有可排序功能。对于为什么会发生这种情况有什么想法吗?

编辑: 我刚刚发现,如果我用 TabbedPanel 替换 AjaxTabbedPanel ,我就不会遇到这个问题。虽然我仍然不确定这是为什么。谁能启发我吗?

    add(new TabbedPanel("tabs", tabs));

I am creating a web page with several tabs. To implement that I am using wicket AjaxTabbedPanel and several AbstractTab. In each tab I have tables with data and I am using a javascript script to make the tables sortable.

public TabbedPage() {
    List<ITab> tabs = new ArrayList<ITab>();
    tabs.add(new AbstractTab(new Model<String>("first tab")) {
        public Panel getPanel(String panelId) {
            return new TablePanel(panelId);
        }
    });

    tabs.add(new AbstractTab(new Model<String>("second tab")) {
        public Panel getPanel(String panelId) {
            return new TablePanel(panelId);
        }
    });

    add(new AjaxTabbedPanel("tabs", tabs));
}

When I load the page the table in the tab selected by default is sortable. However, as soon as I click any of the links to jump to other tabs (including the one of the tab already selected), none of the tables in any of the tabs allows me sort them (including the one that was previously working - the table in the default tab). If I refresh the page I can sort the table (of the tab selected in the moment of the refresh), but as soon as I click in any of links to switch tabs, the tables stop having the sortable capability again. Any ideas of why is this happening?

EDIT:
I just found that if I replace the AjaxTabbedPanel by TabbedPanel I don't have this problem. Although I'm still not sure why is that. Can anyone enlighten me?

    add(new TabbedPanel("tabs", tabs));

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

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

发布评论

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

评论(3

莳間冲淡了誓言ζ 2024-11-05 05:18:26

通过 JavaScript 对表进行排序很可能是使用特定 DOM-Id 调用的函数,并且似乎是在“onLoad”执行的。然后它访问当前显示的表并完成工作。
通过 Ajax 更改面板的内容不会触发“onLoad”,因此该函数不会再次执行。 TabbedPanel 重新加载页面并因此执行您的脚本。
由于动态生成的 DOM-Id,使用 AjaxTabbedPanel 选择以前的可排序表不起作用。

您的解决方案是将 AjaxCallDecorator 添加到 AjaxTabbedPanel 的链接中,或者包含脚本或至少包含对选项卡式面板的函数调用。

至少这是在没有看到任何来源的情况下想到的......

编辑:
您可能想查看 The Wicket Wiki。有关于点击AjaxLink后如何调用js的描述。这正是应该解决您的问题的方法。

摘要:只需添加

link.add(new AttributeAppender("onclick", new Model("myTableSortingScript();"), ";"));

到AjaxTabbedPanel生成的链接中即可。

Sorting the table by JavaScript is most likely a function called with a specific DOM-Id and seems to be executed 'onLoad'. it then accesses the currently displayed table and does it's work.
Changing the content of your Panel by Ajax doesn't trigger 'onLoad' so the function isn't executed again. TabbedPanel reloads the page and therefore executed your script.
Selecting a previous sortable table with AjaxTabbedPanel doesn't work because of the dynamically generated DOM-Ids.

Your solution is to add a AjaxCallDecorator to the links from AjaxTabbedPanel or to include the script or at least the function call to your tabbed panels.

At least this it what comes to mind without seeing any sources...

EDIT:
You might want to look at The Wicket Wiki. There's a description on how to call js after clicking an AjaxLink. That's exactly what should solve your problem.

Summary: Just add

link.add(new AttributeAppender("onclick", new Model("myTableSortingScript();"), ";"));

to the links generated by AjaxTabbedPanel.

っ〆星空下的拥抱 2024-11-05 05:18:26

在 Wicket 6.0 中,您可以在组件的基础上运行 JavaScript:只需为您的组件重写 renderHead(IHeaderResponse response):

@Override
public void renderHead(IHeaderResponse response) {
    super.renderHead(response);

    response.render(new OnLoadHeaderItem("initalizeMe(\"" + getMarkupId() + "\");"));
}

每次 AjaxTabbedPanel 加载组件时都会执行initializeMe(mycomponentId)。这也适用于标准 TabbedPanel。

In Wicket 6.0 you can run JavaScript on a component basis: Just override renderHead(IHeaderResponse response) for your component:

@Override
public void renderHead(IHeaderResponse response) {
    super.renderHead(response);

    response.render(new OnLoadHeaderItem("initalizeMe(\"" + getMarkupId() + "\");"));
}

initializeMe(mycomponentId) is executed every time the component is loaded by the AjaxTabbedPanel. This also works with the standard TabbedPanel.

别靠近我心 2024-11-05 05:18:26

没有真正的想法,因为我不确定代码在做什么,但我的 面板管理器也遇到了类似的问题。基本上,如果您使用内容中的“innerhtml”脚本将 HTML 动态加载到面板(div 或其他元素)中,则不会执行。

为了解决这个问题,我扫描加载的内容中的“脚本”标签,并使用 DOM 方法附加它们 - 这确实会运行脚本并使其可用。我的“加载”方法是:

    // Load Content
Panel.load = function(Content) {

        // "null" the old (to clear it)
    Panel.innerHTML = null;

        // Load the content
    Panel.innerHTML = Content;

        // Load Scripts
    var AllScripts = Panel.getElementsByTagName("script");
    var AllScriptsCnt = AllScripts.length;
    for (var Cnt = 0; Cnt < AllScriptsCnt; Cnt++){
        var CurScript = document.createElement('script');
        CurScript.type = "text/javascript";
        CurScript.text = AllScripts[Cnt].text;
        Panel.appendChild(CurScript);
    };

};

同样,不确定这是否是问题所在,但根据我的经验,这听起来非常符合目标。

No real idea since I'm not sure what the code is doing but I had a similar problem with my Panel Manager. Basically if you dynamically load HTML into a panel (a div or another element) using "innerhtml" script in the content will not be executed.

To get around this I scan the loaded content for "script" tags and append them using the DOM methods - this does run the script and makes it available. My "load" method is:

    // Load Content
Panel.load = function(Content) {

        // "null" the old (to clear it)
    Panel.innerHTML = null;

        // Load the content
    Panel.innerHTML = Content;

        // Load Scripts
    var AllScripts = Panel.getElementsByTagName("script");
    var AllScriptsCnt = AllScripts.length;
    for (var Cnt = 0; Cnt < AllScriptsCnt; Cnt++){
        var CurScript = document.createElement('script');
        CurScript.type = "text/javascript";
        CurScript.text = AllScripts[Cnt].text;
        Panel.appendChild(CurScript);
    };

};

Again, not sure if this is the issue, but it sounds pretty much on target from my experience.

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