Assuming you start by iterating a List of custom objects,
private List<MyCustomObject> myList;
and that your custom object has eg. boolean isChecked()
and String getValue()
methods,
you could use two different Lists in the destination Action: one for the items that were unchecked and now are checked, and another one for items that were checked and now are unchecked:
private List<String> itemsThatHaveBeenChecked;
private List<String> itemsThatHaveBeenUnchecked;
then in the page you should use a classic checkbox to get the checked items, and an hidden input to store the value of the unchecked items, activating its value with javascript as opposite to the checkbox value (that must have no name):
<s:iterator value="myList" status="ctr">
<s:if test="!isChecked()">
<!-- I'm not checked, need to catch only if I will be checked -->
<s:checkbox id = "checkbox_%{#ctr.index}"
fieldValue = "%{getValue()}"
value = "false"
name = "itemsThatHaveBeenChecked" />
</s:if>
<s:else>
<!-- I'm checked, need to catch only if I will be unchecked -->
<input type = "checkbox"
id = "<s:property value="%{'checkbox_'+#ctr.index}"/>"
checked = "checked"
class = "startedChecked" />
<!-- hidden trick -->
<input type = "hidden"
id = "hidden_<s:property value="%{'checkbox_'+#ctr.index}"/>"
value = "<s:property value="%{getValue()}"/>"
name = "itemsThatHaveBeenUnchecked"
disabled = "disabled" />
</s:else>
</s:iterator>
<script>
/* Enable the hidden field when checkbox is unchecked,
Disable the hidden field when checkbox is checked */
$(function() {
$("input[type='checkbox'].startedChecked").change(function () {
$("#hidden_"+this.id).prop({disabled : this.checked});
});
});
</script>
This way you will get only the values needed in both Lists.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…