-1

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.

Community
  • 1
  • 1
D2Gambit
  • 21
  • 6
  • Can you post the markup? If you have set an id on the button in the markup it will trigger all buttons with the same id. in this case alle exportButtons. – papkass Aug 23 '15 at 17:50

2 Answers2

1

Do you really need the form at all? From your code i assume you only need the game object in the process? Why not use a simple AjaxLink:

item.add(new AjaxLink<Games>("exportButton", item.getModel()) {
    public void onClick(AjaxRequestTarget target) {
        // generate export 
    }
});
Imperative
  • 3,138
  • 2
  • 25
  • 40
  • Still the same issue. When I click the export button for this individual item, every onclick is called and my log message is printed out 50+ times. – D2Gambit Aug 23 '15 at 19:46
0

Submit button submits the whole form, i.e. all items in the form will be updated. No matter how many submitting buttons you have - they will do the same.

With https://issues.apache.org/jira/browse/WICKET-5948 (6.21.0+ & 7.1.0+) it will be possible to attach AjaxFormComponentUpdatingBehavior to the item and it will submit the data only of its children components.

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • This did not fix the problem. Clicking export still triggers for every cell item.I tried passing `item`, `item.getModel()`, and `item.getModelObject()`. All are triggering submit for every cell item. – D2Gambit Aug 22 '15 at 07:49