I have a page that has a form backing object that contains a collections property that I loop through on the page. I want to give the user the option of deleting an item of the collection by clicking on a remove button. However I don’t know which remove button was pressed in the controller since each item in the collection has a remove button. Basically I need to know which remove button was pressed so I know what question id should be deleted. What is the best way to go about this? Please see my page below:
<%@ include file="/WEB-INF/jsp/taglibs.jsp" %>
<sf:form method="POST" modelAttribute="surveyInfo" >
<table id="glossarysearchtable-full" border="0" cellpadding="0" cellspacing="0">
<tr align="left">
<td class="searchResultTitle" colspan="2">
Schedule Number ${surveyInfo.surveyNumSch}
<input type="submit" class="small-short inner2" value="Save" alt="Save" title="Save" />
<input type="button" class="small-short inner2" value="Print" alt="Print" title="Print" />
<input type="button" class="small-short inner2" value="Remove" alt="Remove" title="Remove" />
<sf:input type="hidden" path="id" id="id" cssClass="inputbox-survey" maxlength="100" size="100" />
<sf:input type="hidden" path="surveyTitle" id="surveyTitle" cssClass="inputbox-survey" maxlength="100" size="100" />
<sf:input type="hidden" path="surveyName" id="surveyName" cssClass="inputbox-survey" maxlength="100" size="100" />
</tr>
<c:forEach items="${surveyInfo.allSurveyQuestions}" var="surveyQuestion" varStatus="status">
<tr align="left">
<td class="searchResultTitle" colspan="2">
Question ${status.count} <input type="submit" class="small-short inner2" value="Remove${status.count}" alt="Remove" title="Remove" />
<sf:input type="hidden" path="allSurveyQuestions[${status.index}].questionId" id="questionId${status.count}" cssClass="inputbox-survey" maxlength="100" size="100" />
<sf:input type="hidden" path="allSurveyQuestions[${status.index}].id" id="id${status.count}" cssClass="inputbox-survey" maxlength="100" size="100" />
</td>
</tr>
<tr class="altrow" align="left">
<td height="20">Text:</td>
<td><sf:input path="allSurveyQuestions[${status.index}].questionText" id="questionText${status.count}" cssClass="inputbox-survey" maxlength="100" size="100" /></td>
</tr>
</c:forEach>
</table>
</sf:form>
Well I solved my issue, what I did was created a property called isDeleted in my domain object which is also my form backing object and created a bound checkbox for each record on the page. If the user clicks on the checkbox or checkboxes and clicks saves, the controller checks the value of the checkbox and populates 2 different collections one for toBeSaved and another for toBeDeleted. Unchecked records gets saved and checked records get deleted from the database.
I saw lots of options on the web to do this via the jquery route, but I am still learning jquery and preferred to do it server side.