【发布时间】: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。