I followed this post to add click functionality to a button within a ListItem, but for some reason when the form is submitted (button clicked), the button click is happening for EACH item and not the individual one clicked. I can get the preferred result by adding the onClick to the item itself, but I'd rather have the click be registered to a button within the item. How can I achieve a click action on a button that effects ONLY the item?
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 is an image of the listview if that helps. The button is outlined in red.