I have the following form with repeating items:
@(adverts: List[models.AdvertModel])
@if(adverts.size() > 0 && adverts != null) {
@helper.form(action = routes.UserController.editAdvert()) {
@for( (advert, index) <- adverts zip (Stream from 0)) {
<div>@adverts.get(index).title</div>
<button type="submit" name="delete" id="delete_@index">delete</button>
}
}
and this controller:
public Result editAdvert() {
String[] indices = request().body().asFormUrlEncoded().get("delete");
if (indices != null) {
// delete advert
}
return ok();
}
I would like to be able to delete adverts according to their ids but with the current code my array contains a String "delete"
instead of i.e. "delete_0"
.
How do I get the index of the clicked button?