【问题标题】:Wicket Listview button onclick called for each item为每个项目调用 Wicket Listview 按钮 onclick
【发布时间】:2015-08-22 05:45:28
【问题描述】:

我按照this post 向 ListItem 中的按钮添加了单击功能,但由于某种原因,当提交表单(单击按钮)时,按钮单击发生在每个项目而不是单个单击的项目上。我可以通过将 onClick 添加到项目本身来获得首选结果,但我宁愿将单击注册到项目中的按钮。如何在仅影响项目的按钮上实现单击操作?

ListView<Games> gamesList = new ListView<Games>("gamesList", games) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(final ListItem<Games> item) {
            Form<?> form = new Form<>("exportForm");
            final SpecialButton exportButton = new SpecialButton("exportButton", item);
            form.add(exportButton);
            ...
            item.add(form);
        }
}

private class SpecialButton extends AjaxButton {
    final ListItem<Games> item;

    public SpecialButton(final String id, final ListItem<Games> item) {
        super(id);

        this.item = item;
    }

    @Override
    protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
        // here you cand do everything you want with the item and the model object of the item.(row)
        Games game = item.getModelObject();
        System.out.println("Calling file generation with match id: " + game.getGameId()
        + " summoner id: " + game.getSummonerId() 
        + " enemy name: " + game.getEnemyChampName());
    }

}

Here 是列表视图的图像,如果有帮助的话。该按钮以红色标出。

【问题讨论】:

  • 您可以发布标记吗?如果您在标记中的按钮上设置了 id,它将触发具有相同 id 的所有按钮。在这种情况下,所有 exportButtons。

标签: listview onclick wicket


【解决方案1】:

你真的需要这个表格吗?从您的代码中,我假设您在此过程中只需要游戏对象?为什么不使用简单的AjaxLink

item.add(new AjaxLink<Games>("exportButton", item.getModel()) {
    public void onClick(AjaxRequestTarget target) {
        // generate export 
    }
});

【讨论】:

  • 还是同样的问题。当我单击此单个项目的导出按钮时,每次单击都会被调用,并且我的日志消息会被打印 50 多次。
【解决方案2】:

提交按钮提交整个表单,即表单中的所有项目都将被更新。无论您有多少提交按钮 - 它们都会做同样的事情。

使用https://issues.apache.org/jira/browse/WICKET-5948 (6.21.0+ & 7.1.0+) 可以将 AjaxFormComponentUpdatingBehavior 附加到项目,它只会提交其子组件的数据。

【讨论】:

  • 这并没有解决问题。单击导出仍然会触发每个单元格项目。我尝试传递itemitem.getModel()item.getModelObject()。所有人都在为每个单元格项目触发提交。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 1970-01-01
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
相关资源
最近更新 更多