2015-08-22 54 views
-1

我跟着this post添加到ListItem中按鈕的點擊功能,但由於某種原因,當表單被提交(點擊按鈕)時,按鈕點擊發生每個項目,而不是個人點擊。我可以通過將onClick添加到項目本身來獲得首選結果,但我寧願將點擊註冊到項目中的按鈕。我如何才能在僅影響項目的按鈕上實現點擊操作?Wicket Listview按鈕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是一個listview的圖像,如果有幫助。該按鈕用紅色標出。

+0

你可以發佈標記嗎?如果您在標記中的按鈕上設置了一個ID,它將觸發具有相同ID的所有按鈕。在這種情況下,導出按鈕。 – pikand

回答

0

提交按鈕提交整個表單,即表單中的所有項目都將被更新。無論您有多少提交按鈕 - 他們都會這樣做。

隨着https://issues.apache.org/jira/browse/WICKET-5948(6.21.0+ & 7.1.0+)將有可能以AjaxFormComponentUpdatingBehavior重視該項目,將只能提交的其子部件的數據。

+0

這並沒有解決問題。點擊導出仍會觸發每個單元格item.I嘗試傳遞'item','item.getModel()'和'item.getModelObject()'。所有觸發提交每個細胞項目。 – D2Gambit

1

你真的需要表單嗎?從你的代碼我假設你只需要過程中的遊戲對象?爲什麼不使用簡單的AjaxLink

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

仍是同樣的問題。當我點擊這個單獨項目的導出按鈕時,每個onclick都會被調用,並且我的日誌消息被打印出來超過50次。 – D2Gambit

+0

你可能想要提供更多的標記和Java代碼,然後.. – Imperative