How to Dynamically add Ajax Links to a listview

111 views Asked by At

So if I had a listview with each item in the listview having a different Link based on logic that executes in the onClick of AjaxLink how would I dynamically add the wicket:id's for each of the links?

for example:

item.addOrReplace(new ListView<String> ("listViewID" listViewIDs){
  AjaxLink openLinktoPage = new AjaxLink("dynamicAjaxLinkId"){
    @Override public void onClick(AjaxRequestTarget target){
      String url = generateUrlBasedOnDynamicLinkImplementationNotImportant(listViewIDs.getModelObject());
      useUrlToOpenWindowImplementationNotImportant(url);
    }
  }
}

with Html Marked up like so...

<tr wicket:id="listViewIDs">
   <td>
       <a href="not important">Some Link Text Here</a>
   </td>
</tr>

Looks like I am going to have to generate the html dynamically in the code and append it to the html markup. Not sure how to do that though.

1

There are 1 answers

4
martin-g On

No need to generate HTML. This is pretty standard use case for repeaters in Wicket.

item.addOrReplace(new ListView<String> ("listView", listViewIDs) {
  @Override protected void populateItem(final ListItem<String> item) {
     AjaxLink openLinktoPage = new AjaxLink("link"){
       @Override public void onClick(AjaxRequestTarget target){
        // do something that uses item.getModelObject()
        // the name "openLinkToPage" makes me think you may need BookmarkablePageLink instead
       }
     }
     item.add(openLinktoPage);
  }
}

And the markup:

<tr wicket:id="listView">
    <td>
        <a wicket:id="link">Some Link Text Here</a>
    </td>
</tr>