How to get the specifc unchecked checkboxes in Struts2

后端 未结 1 1417
春和景丽
春和景丽 2020-12-20 03:20

In my application I am printing some document along with some checkboxes (Some checkboxes get checked by default when page loads) on some specific action.

Now user

相关标签:
1条回答
  • 2020-12-20 03:51

    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.

    0 讨论(0)
提交回复
热议问题